我正在使用 TableView xib,所有委托(delegate)和数据源似乎都运行良好。
如果我设置 self.textLabel.text
,它会正确显示一般的 tableviews 数量,但我需要显示我的自定义 TableViewCell。
我创建了一个 HistoryCell.xib,其中只有一个 tableviewcell。
我创建了一个 UITableViewCell 类“HistoryCell.h/HistoryCell.m”,并将其设置为 HistoryCell.xib 的文件所有者。
我将 UILabels 连接到 HistoryCell.h
UILabel 状态标签
UILabel 名称标签
UILabel 时间标签
在我的主 ViewController 类中 cellForRowAtIndexPath
我正在投入
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.nameLabel.text = @"Donald Duck";
return cell;
}
我做错了什么?
谢谢!
最佳答案
你应该像这样注册 Nib (可能在 viewDidLoad 中):
[self.tableView registerNib:[UINib nibWithNibName:@"HistoryCell" bundle:nil ] forCellReuseIdentifier:@"historyCellType"];
然后在您的 cellForRowAtIndexPath 方法中,使用这种不需要 if cell == nil 子句的新方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellType" forIndexPath:indexPath];
cell.nameLabel.text = @"Donald Duck";
return cell;
关于ios - 无法显示我的自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15557422/