嵌套的UITableView不返回任何单元格

我有一个UITableView ,其中有另一个UITableView嵌套在其单元格中( 我知道这是不好的做法,不用担心!)。

问题是,当我调用dequeueReusableCellWithIdentifier:我回来了。 但是,当UITableView没有嵌套在另一个内部时,这个工作正常。

有没有办法不重用UITableViewCell ,而是每次直接实例化它?

我试过用这个:

 ContactFieldCell *cell = [[ContactFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:thisCellIdentifier]; 

它不会返回nil,但是我的UITableView没有任何内容!

这是“父”UITableView的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ContactCardCell"; ContactCardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSArray *objects = [[sections objectAtIndex:indexPath.section] objectForKey:@"objects"]; CDCard *card = [objects objectAtIndex:indexPath.row]; cell.delegate = self; cell.fieldsTableView = [[CardTableViewController alloc] initWithCard:card]; [cell.fieldsTableView.view setFrame:CGRectMake(17, 12, 256, 163)]; [cell.contentView addSubview:cell.fieldsTableView.view]; return cell; } 

这里是“子”UITableView的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *thisCellIdentifier = @"ContactFieldCell"; ContactFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:thisCellIdentifier]; cell.delegate = self; cell.field = [self.card.sortedFields objectAtIndex:indexPath.row]; return cell; } 

ContactFieldCell是故事板中的原型单元。 它有以下代码:

 @interface ContactFieldCell : UITableViewCell @property (nonatomic, weak) id delegate; @property (nonatomic, strong) CDField *field; @property (nonatomic, strong) IBOutlet UILabel *displayNameLabel; @end 

UITableView是一个非常强大的元素,可用于构建出色的应用程序。

唯一要记住的是,必须清楚基础知识。 现在从您的代码中,我无法确定您是否正确分配了delegatedataSource ,但我仍然会提到它以防其他人需要它。

你有一个子类UITableViewCell ,它又包含一个UITableViewUIViewController必须是外部UITableViewdelegatedataSource 。 确保已在.h.m文件中设置它。

接下来,您的自定义单元格也必须是delegatedataSource ,但对于内部UITablewView 。 我想在这里,你已经在UITableViewinit方法中创建了内部UITableViewCell 。 将delegatedataSource设置在那里。 然后在drawRect方法中设置其他运行时属性(如果需要)并调用它的reloadData

UIViewController必须覆盖外部表的委托和dataSource方法,并且单元格必须覆盖内部表的方法。

此外,请确保绘制单元格的时间,您的数据不是nilnull

一个非常重要的事实,人们错过了以下代码:

 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } 

只是将单元格出列是不够的。 单元格第一次出列时,它是nil因为它尚未创建。 因此if条件。 一旦分配并初始化并添加到表中,出列代码就会起作用。

注意 :仔细查看您的代码后(抱歉没有第一次查看),我注意到您已经为您的单元分配了一个UITableViewController 。 您认为该单元将如何显示控制器? 请改用UITableView 。 尝试遵循我在第3段中提到的模式。使用自定义单元格中的表作为私有成员(或属性,您的愿望),在init分配它。 从视图控制器将数据分配给单元格。 然后使用此数据在其drawRect设置内部表视图单元格的属性。 它应该工作正常。

dequeueReusableCellWithIdentifier:如果没有找到出列的单元格,则不会创建单元格。

手动创建单元格,或使用dequeueReusableCellWithIdentifier:forIndexPath:

是的 – @vikingosegundo是正确的,但要扩展他的答案,你还需要先注册你的单元格。 dequeueReusableCellWithIdentifier:可能返回nil。 如果你需要创建你的单元格,但是dequeueReusableCellWithIdentifier: forIndexPath:将始终返回一个有效的单元格,你需要告诉它是什么类型的单元格,这就是registerClass作用。

为UITableViews执行此操作。

 - (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[ContactFieldCell class] forCellReuseIdentifier:@"ContactFieldCell"]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *thisCellIdentifier = @"ContactFieldCell"; ContactFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:thisCellIdentifier forIndexPath:indexPath]; cell.delegate = self; cell.field = [self.card.sortedFields objectAtIndex:indexPath.row]; return cell; }