【发布时间】:2012-12-29 01:20:11
【问题描述】:
我通过以下步骤在我的 iPhone 应用中创建了一个自定义表格视图单元格。
- 在我的故事板中,我创建了一个示例单元格,将
UILabel和UIImageView拖入。 - 添加了新文件,我创建了
UITableViewCell的子类。 - 在 Interface Builder 中,我选择了我的单元格并将其类指定为我刚刚在步骤 2 中创建的类。
- 在我的自定义表格视图单元格的代码中,我创建了两个 IBOutlet 属性并将它们连接到我的故事板中的
UILabel和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不应该初始化这些吗?如果没有,我应该在哪里做呢?
【问题讨论】:
-
您确定情节提要中该单元格的重用标识符完全等于“item_id”的值吗?
-
不……我刚刚发现了它……我在不同的地方有不同的重用标识符。谢谢。
标签: ios uitableview storyboard