在此屏幕截图中,您可以看到我已在 UIViewController 中添加了 UITableView,然后通过在其中添加一些标签来自定义 UITableViewCell。但问题是当我运行应用程序时。所有单元格都是空的。根本没有标签。
我不明白可能是什么问题。我已经在网上搜索并阅读了教程,但无法解决该问题。
经过一番努力,我自己解决了这个问题。
实际上,当您创建自定义单元格时,您必须执行以下操作:
- 在故事板单元上设置标签、图像等。
- 创建一个自定义单元格类(继承自 UITableViewCell)(CustomCell.h & .m),CustomCell.h 具有标签、图像等的 iboutlet 的所有属性,并在实现中综合它们。
- 创建此自定义类后,返回故事板,选择自定义单元格,将其类更改为 CustomCell 并为其提供一些标识符,例如“MyCustomCell”,然后右键单击自定义单元格并使用标签等连接 IBOutlet。
-
现在,在您实现 UITableView 的类中导入 CustomCell 类,并使用您在 CustomCell 类中定义的属性。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyCustomCell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
cell.myCustomLabel.text = @"My Text";
return cell;
}
我完成了这一切,我的问题得到了解决,请不要忘记连接代表、数据源和表。
希望这会帮助其他人。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)