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

标签 ios objective-c uitableview scroll

虽然网站上有几篇关于滚动时 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/

相关文章:

ios - 如何停止在横向模式下使用单元格向上滚动表格 View 标题?

objective-c - 如何在 objective-c 项目中使用纯 C 文件?

ios - 如何在 iOS 中使用 AddressBook 获取联系人的创建日期

ios - tableView 中的搜索和复选标记

ios - UnitTest - 等待期望被 NSPredicate 忽略

ios 快速写入流

ios - 我可以使用相同的 NSString 作为 @synchronized 的一个信号量吗

ios - 删除应用程序时如何清除所有计划的本地通知

iphone - 代码 : Horizontal Scrolling in a TableViewCell

ios - 为唯一日期创建UITableView节