gpt4 book ai didi

ios - 如何正确初始化自定义 UITableviewCell?

转载 作者:IT王子 更新时间:2023-10-29 07:49:52 27 4
gpt4 key购买 nike

我使用以下两种方法返回自定义单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *key = [self keyForIndexPath:indexPath];
UITableViewCell *cell;

if ([key isEqualToString:DoneButtonCellKey]) {
cell = [self [self doneButtonCellForIndexPath:indexPath];
return cell;
} else {
//code to return default cell...
}
}

然后:

- (DoneButtonCell *)doneButtonCellForIndexPath: (NSIndexPath *)indexPath {

DoneButtonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DoneButtonCellIdentifier forIndexPath:indexPath];
return cell;

}

在这里单元格使用的正确初始化方法是什么,以便我可以在单元格初始化时更改单元格的某些属性?

编辑:我发现了问题,因为没有为我调用 init/awakeFromNib 方法。我找到了错误,这是因为我没有将“自定义类”从 UITableViewCell 更改为我的自定义类。现在 awakeFromNib 和 initWithCoder 的工作方式如下所述。

最佳答案

您可以在 DoneButtonCell 的类中进行更改,或者在

- (void)awakeFromNib
{
.. essential to call super ..
super.awakeFromNib()
//Changes done directly here, we have an object
}

或者 initWithCoder: 方法:

-(id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];

if(self)
{
//Changes here after init'ing self
}

return self;
}

关于ios - 如何正确初始化自定义 UITableviewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20725151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com