【发布时间】:2014-07-29 08:13:25
【问题描述】:
我正在尝试为UITableViewCell 使用自定义 XIB 文件,但无论我做什么,表格单元格始终是空的 - 我的 XIB 根本没有加载。
没有任何错误。
这是我的工作:
创建一个空的应用程序,添加一个故事板,表格视图控制器,添加一个新的
UITableViewController,将它与表格挂钩并设置一些示例项目。创建一个控制器作为
UITableViewCell的子类+检查选项以生成XIB。创建三个示例标签并将它们添加为
IBOutlets。加载 XIB 文件并尝试将其设置为单元格。
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
items = [[NSMutableArray alloc] init];
for (int i = 0; i < 20; i++) {
ItemObject *item = [ItemObject new];
item.topLabel = [NSString stringWithFormat:@"Top %d", i];
item.leftLabel = [NSString stringWithFormat:@"Left %d", i];
item.rightLabel = [NSString stringWithFormat:@"Right %d", i];
[items addObject:item];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ItemCell";
// Set up the cell.
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
}
ItemObject *currentObject = [items objectAtIndex:indexPath.row];
cell.topLabel.text = currentObject.topLabel;
cell.leftLabel.text = currentObject.leftLabel;
cell.rightLabel.text = currentObject.rightLabel;
// General cell and table settings.
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[tableView setSeparatorColor:[UIColor clearColor]];
return cell;
}
错误可能在哪里?有人能指出我正确的方向吗?
谢谢!
【问题讨论】:
-
阅读有关
registerNib:方法的文档。你会明白你做错了什么。 -
@duci9y 刚读了几遍,但我不太明白;你能澄清一下吗?这将有助于我在未来了解这些问题。
-
来自文档:在将任何单元格出列之前 调用此方法或 registerClass:forCellReuseIdentifier: 方法来告诉表视图如何创建新单元格。
-
感谢您的澄清 - 这是我期望的部分,但不确定。将记住这一点以供将来参考:D
标签: ios objective-c uitableview