【发布时间】:2014-02-16 11:34:58
【问题描述】:
我正在使用 Xcode 5,并正在开发针对 iOS6/7 的应用程序。
在我的故事板中,引用自定义类“ObjListCell”的“原型单元格”,派生自“UITableViewCell”,包含一个“内容视图”,而该“内容视图”又包含一个 UIImageView 和两个 UILabel 元素。他们三个都连接到我的 ObjListCell 类中的一个 IBOutlet。
表格视图控制器是指我的自定义类“ObjListViewController”,派生自“UITableViewController”。创建此视图控制器时,我已经检索了数据,因此在 viewDidLoad 中我立即调用[self.tableView reloadData];。但在此之前,我注册了单元格类:[self.tableView registerClass: [ObjListCell class] forCellReuseIdentifier:@"objTableCell"];,并且在情节提要中将“objTableCell”设置为单元格的标识符。
部分中的方法 numberOfRows 返回 14,实际上我在我的 iPhone 上看到了一个包含 14 个空行的列表。正如我从日志中看到的,实际上调用了方法 cellForRowAtIndexPath,并且方法 dequeueReusableCellWithIdentifier 返回了一个非零单元格。于是我设置了图标和标签内容:
cell.mCellIcon.image = icon;
cell.mCellTitle.text = title;
cell.mCellExtra.text = extra;
我确定图标和文本都可以,因为我在分配它们之前就记录了它们。 但是,我的表格单元格显示为空。
谁能提出问题出在哪里?
这是 cellForRowAtIndexPath 的完整代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"objTableCell";
ObjListCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
NSLog(@"ObjListViewController ERROR: Cell is nil");
// Retrieve obj for this cell
Obj* obj = [mObjs objectAtIndex:indexPath.row];
if (obj == nil)
NSLog(@"ObjListViewController ERROR: Obj not found for cell; indexPath = %i", indexPath.row);
NSLog(@"ObjListViewController DEBUG: title = %@, extra = %@", obj.mTitle, obj.mExtra);
// Set cell icon
UIImage *icon = [mIcons iconForId:obj.mId andSize:@"big" withDefault:@"defaultIcon"];
if (icon == nil)
NSLog(@"ObjListViewController ERROR: Icon not found for cell; indexPath = %i", indexPath.row);
cell.mCellIcon.image = icon;
// Set cell title and extra
cell.mCellTitle.text = obj.mTitle;
if (obj.hasExtra)
cell.mCellExtra.text = obj.mExtra;
NSLog(@"ObjListViewController DEBUG: Inspecting cell content; icon = %@, title = %@, extra = %@",
(cell.mCellIcon.image != nil ? @"OK" : @"nil"), cell.mCellTitle.text, cell.mCellExtra.text);
// Standard accessory symbol
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
这是日志的摘录:
2014-02-16 15:21:06.665 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: title = Valle d'Aosta, extra =
2014-02-16 15:21:06.668 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: Inspecting cell content; icon = nil, title = (null), extra = (null)
2014-02-16 15:21:06.685 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: title = Provincia di Belluno, extra =
2014-02-16 15:21:06.690 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: Inspecting cell content; icon = nil, title = (null), extra = (null)
日志中没有出现任何“ERROR”日志行。
【问题讨论】:
-
您是否将单元格的类更改为您在 IB 中的自定义类?
-
是的,我做到了。在故事板中,我的意思是。我刚刚发现我没有在我的 ObjListCell.m 文件中合成三个 IBOutlets,但是在修复这个问题之后问题仍然存在。我还添加了一个日志来检查 cell.mCellIcon.image、cell.mCellTitle.text 和 cell.mCellExtra.text 的值,在为它们分配值之后:它们是零!但正如我所说,赋值前的右手数据是可以的。
-
添加
NSLog(@"%@ %@ %@", cell.mCellIcon, cell.mCellTitle, cell.mCellExtra);。如果它将显示 nil 值 - 您忘记将 GUI 元素绑定到 IBOtlet。 -
实际上它们是 nil,但在故事板中,三个 UI 元素确实有一个引用出口,例如 mCellIcon 引用 ObjListCell。当我检查 ObjListCell.h 文件中的 IBOutlets 时,每个左侧出现一个实心点;这意味着它们已正确连接,我猜。
标签: ios uitableview