表视图控制器

时间:2015-08-17 12:37:28

标签: ios objective-c uitableview

我正在使用故事板来创建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]返回一个字符串。

3 个答案:

答案 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)

确保已完成此步骤:

  1. 为自定义单元格定义新类,必须为UITableViewCell类型
  2. 在自定义单元格类
  3. 中为标签添加IBOutlet属性
  4. 在outlet属性和标签之间建立连接
  5. 通过选择原型单元格设置自定义单元格的类类型,然后进入Identity Inspector
  6. 更改代码以引用自定义单元格类型而不是UITableViewCell
  7. 此外,您应使用CellIdentifier代替RestorationID