如何加载自定义UITableViewCell而不使用笔尖名称
问题描述:
我正在设计我的应用程序,根据设计tableView应该根据可重用标识符加载任何自定义单元格。如何加载自定义UITableViewCell而不使用笔尖名称
从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;
}
人们可以看到我使用相同标识符笔尖名字为好。 当可重复使用的标识符和笔尖名称相同时,它将正常工作,但如果笔尖名称与可重复使用的标识符不同,它将会崩溃。
任何人都可以建议如何删除这个依赖的笔尖名称。
答
注册您的电池类
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:cellIdentifier];
而在你的cellForRowAtIndexPath
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
答
您可以设置标识的.xib文件。
CustomCell是未知的..关于单元格的唯一信息是标识符 –
请参阅编辑答案。 –
我已经试过了..加载空白单元格 –