gpt4 book ai didi

ios - 优化多个单元格的 CellForRow 方法

转载 作者:行者123 更新时间:2023-11-29 01:20:33 24 4
gpt4 key购买 nike

如果我们在两个单元格中确实有相同的内容(例如 2 个标签和 1 个 ImageView ),谁能帮助我优化以下代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == booksTbl) { // Your conditions here to choose between Books and Movies
BookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookCell"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bookCell"];
}
return cell;
} else {
MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"movieCell"];
}
return cell;
}

我尝试将 UITablewViewCell 作为常见的并根据条件,我将其与适当的单元格一起转换。但这对我不起作用。

最佳答案

试试这个:

BookCell *cell = (BookCell*)[tableView dequeueReusableCellWithIdentifier:@"bookCell"];


if(cell == nil){
// NSLog(@"---Cell Allocated Memory---");
cell = (BookCell *)[[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:nil options:nil] lastObject];
[cell setRestorationIdentifier:@"bookCell"];
}

关于ios - 优化多个单元格的 CellForRow 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650382/

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