如何在不使用nib名称的情况下加载自定义UITableViewCell

时间:2016-07-27 13:49:36

标签: ios iphone uitableview

我正在设计我的应用程序,根据设计,tableView应该根据可重用的标识符加载任何自定义单元格。

从webservice tableView将获得可重用标识符的列表,tableView将根据标识符加载所有单元格。

这是我的cellForRowAtIndex方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[_cardsIdentifier objectAtIndex:indexPath.row]];//_cardsIdentifier is array of identifiers 
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:[_cardsIdentifier objectAtIndex:indexPath.row] owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }
    NSLog(@"%@",cell.reuseIdentifier);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;
}

正如我可以看到我对nib名称也使用相同的标识符。 当可重用标识符和nib名称相同时,它将正常工作,但如果nib名称与可重用标识符不同,它将崩溃。

任何人都可以建议我如何删除这个笔尖名称的依赖。

2 个答案:

答案 0 :(得分:0)

注册您的手机课

[self.tableView registerClass:[UITableViewCell class]
       forCellReuseIdentifier:cellIdentifier];

在你的cellForRowAtIndexPath

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

答案 1 :(得分:0)

您可以在.xib文件中设置标识。

相关问题