【问题标题】:How to add a tableView into a CellView in objective c?如何在目标 c 中将 tableView 添加到 CellView 中?
【发布时间】:2016-08-24 18:02:42
【问题描述】:

我想在一个单元格中显示一个 tableView:

 _____________________________________
 | (IMAGE) ______________              | --CELL 0   

 |         | MyCustomText|--CELL 0.0   |
 |         |_____________|             |
 |         |MyCustomText2|--CELL 0.1   |
 |         |_____________|             |
 |                                     |
 |                                     |
  --------------------------------------
  _____________________________________
 |  (IMAGE) 2                           | --CELL 1
  --------------------------------------

我正在尝试在情节提要中添加一个 tableView,然后连接一个新的 TableviewController 和一个新的 CustomCellTableView,但这不会在该行的表格中显示任何内容。

有可能吗?如何声明 viewController 或者不需要添加 ViewController?

谢谢!

【问题讨论】:

  • 您是否需要 cell0 的内容可以滚动(没有 tableView 的其余部分滚动)?如果没有,UITableView 可以管理不同类型的单元格。您可以有两个自定义单元格,一个用于 cell0,另一个用于 celle1。
  • 问题需要稍微清楚一点,请添加示例图片

标签: ios objective-c uitableview


【解决方案1】:

我认为您不需要新的 tableview 控制器。在您的表格单元格中,您可以添加另一个表格视图。

@interface ImageCell : UITableViewCell<UITableViewDataSource,UITableViewDelegate>

@property (retain, nonatomic) IBOutlet UITableView *abc;

@end

并像往常一样实现数据源和委托方法。

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

【讨论】:

    【解决方案2】:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (tableView == self.tableView) {
            return 8;
        } else {
            return 3;
        }
     }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell;
        if (tableView == self.tableView) {
            cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            if (indexPath.row == 0) {
                self.childTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 100)];
                self.childTableView.delegate = self;
                self.childTableView.dataSource = self;
                [cell.contentView addSubview:self.childTableView];
            }else {
                cell.textLabel.text = @"Pavan";
            }
        } else if (tableView == self.childTableView){
            cell = [self.tableView dequeueReusableCellWithIdentifier:@"childCell"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"childCell"];
            }
            cell.textLabel.text = @"hi";
        }
        return  cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == self.tableView) {
            if (indexPath.row == 0) {
                return 250;
            } else {
                return 100;
            }
        } else {
            return 50;
        }
    }
    

    【讨论】:

    • 第二个 TableView 的数据源和第一个 TableView 一样吗?对我来说,如果我将数据源设置为 ViewController,应用程序就会崩溃。
    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多