OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Table View Controller 中显示 nil 的原型(prototype)单元格标签 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:32
标题: ios - 在 Table View Controller 中显示 nil 的原型(prototype)单元格标签

我正在使用 Storyboard 来创建表格 View 并使用原型(prototype)单元格。 原型(prototype)单元格包含一个文本标签

TableView Controller :

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;

细胞.h

@interface Cell : UITableViewCell

@property (nonatomic,weak) IBOutlet UILabel* nameLabel;

我在 viewcontroller.m 中导入了 cell.h。
我将原型(prototype)单元格的 restoreID 命名为“Cell”,原型(prototype)单元格的类命名为“Cell”,并将原型(prototype)单元格中的 textlabel 链接到 nameLabel。

问题是:
cell.nameLabel.text 总是返回 nil 值,即使 [array objectAtIndex:indexPath.row] 返回一个字符串。



Best Answer-推荐答案


尝试命名 CellIdentifier 而不是 restoreID。

CellIdentifier 是一种标记 tableViewCells 的方法。这样 uitableView 在需要复用的时候就可以知道要拉出哪个 uitableviewcell。

关于ios - 在 Table View Controller 中显示 nil 的原型(prototype)单元格标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050620/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4