我有一个从 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/