iOS:没有 reuseIdentifier 的 nib 自定义 UITableViewCell?

标签 ios uitableview

我有一个从 nib 加载的自定义 UITableViewCell。我可以通过执行以下步骤使用 reuseIdentifier 将它拉入我的应用程序:

1)在Nib中设置重用标识符为“CustomCellIndentifier”

2)注册 Nib :

[[self tableView] registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"CustomCellIndentifier"];

3) 返回tableview中的cell:cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellIndentifier"];
     return cell;
}

我的问题是,如何返回没有重用标识符的单元格?我有一张小 table ,我不希望单元格被重复使用(如果我滚动,它会弄乱单元格中的一些 subview )。

我已经尝试将上述 3 个重用标识符组合设置为 nil,但它们都会产生错误。

我也在下面尝试过这个,但是如果我滚动经过单元格并且我收到一条错误消息“没有重复使用的表格单元格的索引路径”,单元格内容会重置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
     if (cell == nil) {
           NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
           cell = [topLevelObjects objectAtIndex:0];
     }

     return cell;
}

最佳答案

我认为这不是一个好方法,但根据您的要求使用这个:

CustomCell 类中编写init 方法

- (id)init
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"QuestionOptionCell" owner:self options:nil];
        CustomCell *theEditView = [nibObjects objectAtIndex:0];
        self = theEditView;
        theEditView = nil;
        return self
    }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CustomCell *cell = [[CustomCell alloc] init];
     if (cell == nil) {
           NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
           cell = [topLevelObjects objectAtIndex:0];
     }

     return cell;
}

关于iOS:没有 reuseIdentifier 的 nib 自定义 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17078840/

相关文章:

android - 如何在 flutter 中使下拉项的宽度大于容器(下拉按钮)

swift - 调整容器 View 的高度以匹配 subview 的高度

ios - 在 Swift 中将多个项目约束在屏幕中央

android - Flutter 的 'path_provider' 依赖项中的未定义类

ios - UITableView 动画无法正常工作

css - Javafx Tableview 如何为具有特定值的单元格着色

ios - 获取此单元格中单元格或文本字段的位置

ios - 为什么 UITableview 数据重叠?

ios - 如何避免将外部URL硬编码到iOS App中?

ios - 在 UILabel 中,是否可以强制一行不在斜线处断开?