我在自定义单元格内使用表格 View 。下面的代码指定了自定义单元格内的表格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"commnetCell";
commnetCell *cell = (commnetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure Cell
[cell.lbl_comment setText:@"kool"];
return cell;
}
此处单元格返回 nill。我已设置委托(delegate)和数据源。
最佳答案
如果重用队列中没有单元格,则必须分配单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell.
return cell;
}
或者,您可以在 Storyboard中创建原型(prototype)或注册自定义单元格类。然后,您不必自己分配它。当您使用 dequeueReusableCellWithIdentifier
方法请求它时,它将为您创建。
dequeueReusableCellWithIdentifier:
将始终返回一个单元格,如果您使用相应的标识符调用了 registerClass:
。
关于ios - 自定义单元格内的 uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23029054/