我通过以下步骤在我的 iPhone 应用程序中创建了一个自定义表格视图单元格。
- 在我的故事板中,我创建了一个示例单元格,将其拖入
UILabel
and a UIImageView
.
- 添加了新文件,我将其作为其子类
UITableViewCell
.
- 在 Interface Builder 中,我选择了单元格,并将其类指定为我刚刚在步骤 2 中创建的类。
- 在我的自定义表视图单元格的代码中,我创建了两个 IBOutlet 属性并将它们连接到我的
UILabel
and UIImageView
在故事板中。
-
我的自定义表视图单元格还包含一个方法,它接收另一个对象并从中设置自己的属性:
-(void)populateWithItem:(PLEItem *)item
{
if (item.state == PLEPendingItem) {
status.text = @"Pending upload..."; //status is a UILabel IBOutlet property
}
else if(item.state == PLEUploadingItem)
{
status.text = @"Uploading...";
}
imageView.image = [UIImage imageWithContentsOfFile:item.path]; //imageView is the UIImageView IBOutlet property
}
这个方法是从我的tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath as follows:
PLEPendingItemCell* cell = (PLEPendingItemCell*)[tableView dequeueReusableCellWithIdentifier:item_id];
if (cell == nil) {
cell = [[PLEPendingItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pending_id];
}
[cell populateWithItem:((PLEItem*)[itemList objectAtIndex:indexPath.row])];
return cell;
问题是单元格总是显示为空。我在 populateWithItem 中设置了一个断点,并意识到状态UILabel
和图像UIImageView
该方法内为零。
IB不应该初始化这些吗?如果没有,我应该在哪里做?
如果您要在故事板中设置单元格,则始终需要使用以下命令创建单元格tableView:dequeueReusableCellWithIdentifier:forIndexPath:
因为这是故事板创建单元并连接视图的地方。
直接使用其构造函数创建单元格(如示例代码中所示)不会从故事板、笔尖等加载任何子视图。您创建的类对故事板原型单元格一无所知。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)