gpt4 book ai didi

ios - UITableView 在滚动后将单元格内容放在错误的行上

转载 作者:行者123 更新时间:2023-11-28 18:11:41 26 4
gpt4 key购买 nike

虽然网站上有几篇关于滚动时 UITableview 单元格重复的帖子,但我找不到解决方案,也许是因为我对 obj-c 还很陌生。

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

NSString *cellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];

UILabel *firstlabel = nil;
UILabel *secondlabel = nil;
UILabel *thirdlabel = nil;

myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(!cell) {
cell = [[myTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

if (noOfRows < [_eachrow count]-1) {

_eachcolumn = [_eachrow[noOfRows] componentsSeparatedByString:@"|"];
firstlabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 142.0,tableView.rowHeight)];
firstlabel.font = [UIFont fontWithName:@"Verdana" size:12];
firstlabel.text = _eachcolumn[0];
firstlabel.textAlignment = ALIGN_LEFT;
firstlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:firstlabel];

secondlabel = [[UILabel alloc] initWithFrame:CGRectMake(142.0, 0, 50.0,tableView.rowHeight)];
secondlabel.font = [UIFont fontWithName:@"Verdana" size:12];
secondlabel.text = _eachcolumn[1];
secondlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondlabel];

thirdlabel = [[UILabel alloc] initWithFrame:CGRectMake(192.0, 0, 60.0,tableView.rowHeight)];
thirdlabel.font = [UIFont fontWithName:@"Verdana" size:12];
thirdlabel.text = _eachcolumn[2];
thirdlabel.textAlignment = ALIGN_CENTER;
thirdlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:thirdlabel];

noOfRows++;
return cell;
}
else return cell;

}

所以,我的问题是,当我向上或向下滚动时,单元格一直放置在错误的行中,没有模式。请帮我解决这个问题。提前致谢

最佳答案

出现此问题是因为单元格是可重用的,您每次都需要覆盖 cellForRow 中的值。

试试这个:

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

NSString *cellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];



myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[myTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

UILabel *firstlabel = nil;
UILabel *secondlabel = nil;
UILabel *thirdlabel = nil;
_eachcolumn = [_eachrow[noOfRows] componentsSeparatedByString:@"|"];
firstlabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 142.0,tableView.rowHeight)];
firstlabel.font = [UIFont fontWithName:@"Verdana" size:12];
firstlabel.text = _eachcolumn[0];
firstlabel.textAlignment = ALIGN_LEFT;
firstlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
firstlabel.tag = 10;
[cell.contentView addSubview:firstlabel];

secondlabel = [[UILabel alloc] initWithFrame:CGRectMake(142.0, 0, 50.0,tableView.rowHeight)];
secondlabel.font = [UIFont fontWithName:@"Verdana" size:12];
secondlabel.text = _eachcolumn[1];
secondlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
secondlabel.tag = 20;
[cell.contentView addSubview:secondlabel];

thirdlabel = [[UILabel alloc] initWithFrame:CGRectMake(192.0, 0, 60.0,tableView.rowHeight)];
thirdlabel.font = [UIFont fontWithName:@"Verdana" size:12];
thirdlabel.text = _eachcolumn[2];
thirdlabel.textAlignment = ALIGN_CENTER;
thirdlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
thirdlabel.tag = 30;
[cell.contentView addSubview:thirdlabel];


return cell;
}
else
{

((UILabel *)[cell.contentView viewWithTag:10]).text = _eachcolumn[1];
((UILabel *)[cell.contentView viewWithTag:20]).text = _eachcolumn[2];
((UILabel *)[cell.contentView viewWithTag:30]).text = _eachcolumn[3];

return cell;
}

关于ios - UITableView 在滚动后将单元格内容放在错误的行上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17536922/

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