ios - 无法显示我的自定义 UITableViewCell

标签 ios objective-c uitableview

我正在使用 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/

相关文章:

ios - 显示了 uitextfield 但未显示按钮

android - ajax jquery 代码不适用于 android -Phonegap-

iphone - 在为其 UIAppearance 设置样式时如何定位特定的 UIView?

ios - 在 IOS 中从 google map place api 中提取需要信息

iphone - 如何比较两个 NSIndexPaths?

objective-c - UI颜色对比

ios - 在用户定义的函数中更改单元格的附件 View

ios - 为什么从 ViewController 转到 TableViewController 时我会不断收到此错误?

iphone - 使用 Core Data 时如何覆盖属性上的 getter?

iphone - 如何动态更改tableview ipad中的行高