虽然网站上有几篇关于滚动时 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;
}
所以,我的问题是当我向上或向下滚动时,单元格一直放置在错误的行中而没有模式。请帮我解决这个问题。提前致谢