我想从已弃用的 TableView 中更新initWithFrame:reuseIdentifier:
.
我的表格视图使用自定义单元格。
到处都说要使用initWithStyle:
,并且它不会以任何方式改变行为或细胞initWithFrame:CGRectZero reuseIdentifier:
.
但是当我用initWithStyle:UITableViewCellStyleDefault reuseIdentifier:
单元格变为空白(即我们的自定义单元格不起作用(因为它是用某种样式初始化的?))。
单元格初始化后(如果它没有出列),我们在单元格上设置文本。但我使用时没有设置这些initWithStyle:reuseIdentifier:
但它适用于initWithFrame:CGRectZero
。除了使用的 init 方法之外,没有更改任何代码(initWithStyle
).
这些行在创建(或重用)单元格后放入:
cell.newsItemNameLabel.text = @"test";
NSLog(@"NewsItemName: %@",cell.newsItemNameLabel.text);
结果为“NewsItemName: (null)”
有人有主意吗?两者之间真正的区别是什么?
谢谢
您的实施cellForRowAtIndexPath
应类似于以下内容:
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
CustomCell *cell = (CustomCell *)(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
return cell;
}
where CustomCell
是您的自定义单元格的类的名称。请注意,此实现使用 ARC(自动引用计数)。如果您不巧使用此功能,请添加autorelease
打电话给你分配的小区。
CustomCell
's initWithStyle
执行:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//do things
}
return self;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)