ios - 自定义单元格内的 uitableview

标签 ios uitableview

我在自定义单元格内使用表格 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/

相关文章:

ios - NavigationBar 在运行时隐藏 View ,但不在 Interface Builder 中

html - 导航栏大标题-动画问题

ios - 你如何防止 UITableViewCellSelectionStyleNone 删除单元格分隔符

ios - 在 UITableView 中实现 UISearchController 以过滤 User 对象数组

ios - MBProgressHUD 阻止用户与 TableView 的交互

ios - Sharepoint 2010,HTML5 视频无法在 iPad 上运行

iOS:画图

ios - 有没有办法快速抓取多个选定表格 View 单元格的文本和详细文本标签?

ios - 自定义表格单元委托(delegate)

ios - 单击时在 JW Player 中播放的 YouTube 视频的简单库