使用XIB文件加载UITableViewCell子类 [英] Loading a UITableViewCell subclass using a XIB file
问题描述
我无法在我的表视图中显示 CustomTableViewCell
, UITableViewCell
的子类。
I am having trouble getting my CustomTableViewCell
, a subclass of UITableViewCell
to appear in my table view.
我使用xib来表示该单元格,但我假设数据源委托的代码没有改变。我确保在表视图单元格XIB中设置相同的重用标识符。
I am using a xib to represent that cell, but I am assuming that the code for the data source delegate doesn't change. I made sure to set an identical reuse identifier inside the table view cell XIB.
我将问题隔离到返回表格单元格的数据源方法无法正常工作的事实,这里是:
I isolated the problem to the fact that the datasource method that returns the table cell isn't working correctly, here it is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
DataObject *foo = [self.dataArray objectAtIndex:indexPath.row];
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell overview] setText:foo.overview];
[[cell price] setText:foo.price];
NSLog(@"cell initialized, description text is %@",cell.overview.text);
return cell;
}
不确定为什么这不起作用,但最后一条日志语句总是打印最后是一个(null),是的,我确实验证了数据对象的 overview
属性中有一个有效的字符串。同样的事情价格
。
Not sure why this isn't working, but that last log statement always prints a (null) at the end, and yes I did verify that the overview
property of the data object has a valid string in it. Same thing for price
.
推荐答案
在.h文件中,你写的它。
In .h file, you write it.
IBOulet CustomTableViewCell *tableCell;
并连接到CustomTableViewCell的.xib文件中的文件所有者。
and connect to File's Owner in CustomTableViewCell's .xib file.
你可以在(UITableViewCell *)tableView中修改它:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
cell = tableCell;
}
我认为这会对你有所帮助。
I think it will be helpful to you.
这篇关于使用XIB文件加载UITableViewCell子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!