gpt4 book ai didi

ios - UItableView打印多条数据

转载 作者:行者123 更新时间:2023-11-29 05:05:00 25 4
gpt4 key购买 nike

我正在我的应用程序中创建分组表。效果很好。但是,当我向上和向后滚动表格时,会在前一个单元格上重新打印文本。看起来很可怕。

这是我的代码

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];

if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease];
}
/*
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
*/

if(indexPath.section == 0)
{
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;

float rowHeight = [DetailViewController calculateHeightOfTextFromWidth:[dict objectForKey:@"golf_name"] :[UIFont boldSystemFontOfSize:18] :290 :UILineBreakModeTailTruncation];

UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 290, rowHeight)];
categorName.font = [UIFont boldSystemFontOfSize:20];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor whiteColor];
categorName.text = [dict objectForKey:@"golf_name"];
[cell addSubview:categorName];
[categorName release];
}
if(indexPath.section == 1)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;

UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 70, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = @"Phone";
[cell addSubview:categorName];
[categorName release];

UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 290, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:@"golf_phone"];
[cell addSubview:phone];
[phone release];
}
if(indexPath.section == 2)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;

UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 90, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = @"Home page";
[cell addSubview:categorName];
[categorName release];

UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 230, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:@"golf_url"];
[cell addSubview:phone];
[phone release];
}
.......
.....
return cell;
}

最佳答案

更新

我原来的答案不是重用 UITableViewCell 的正确方法。您永远不应该使用“CellID%d%d”(indexPath.row&indexPath.column)作为单元格标识符,这违背了单元格重用的真正目的。它将为表中的每一行创建单独的 UITableViewCell,并且每个单元格都保留在内存中。想象一下,如果您有一个大约 1000 行的 tableView 会发生什么。

OP 问题的真正答案是 - 确保重置由 dequeueReusableCellWithIdentifier 返回的 UITableViewCell,因为它可能返回已使用且已填充 UI 的单元格。例如,如果您没有重置 UILabel 中的文本,它可能包含本应显示在 tableView 的不同行中的字符串。

我原来的答案(不要使用这个)

海伦,试试吧,

UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:@"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
}

而不是

UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease];
}

请阅读this ..您将了解如何给出单元格标识符..

关于ios - UItableView打印多条数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5754584/

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