我正在使用故事板来创建tableview并使用原型单元格。 原型单元格包含一个文本标签
TableViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSLog(@"cellIdentifier = %@",cell);
if (cell == nil) {
cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
NSLog(@"cellIdentifier = %@",cell);
}
cell.nameLabel.text = [array objectAtIndex:indexPath.row];
return cell;
cell.h
@interface Cell : UITableViewCell
@property (nonatomic,weak) IBOutlet UILabel* nameLabel;
我在viewcontroller.m中导入了cell.h 我将原型单元的restoreID命名为'Cell',将原型单元的类命名为'Cell',并将原型单元格中的textlabel链接到nameLabel。
问题是:
cell.nameLabel.text
始终返回nil值,即使[array objectAtIndex:indexPath.row]
返回一个字符串。
答案 0 :(得分:4)
尝试命名CellIdentifier而不是restoreID。
CellIdentifier是一种标记tableViewCells的方法。这样,uitableView可以在需要重用它时知道要拉出哪个uitableviewcell。
答案 1 :(得分:0)
您应该设置单元格ReuseIdentifier
而不是RestorationID
。我很好奇你没有设置ReuseIdentifier
并且没有崩溃:)
顺便说一句,如果您使用带有xib的自定义单元格,则应始终将其注册到viewDidLoad
中的UITableView实例:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellReuseIdentifier:@"Cell"];
}
答案 2 :(得分:0)
确保已完成此步骤:
UITableViewCell
类型Identity Inspector
此外,您应使用CellIdentifier
代替RestorationID
。