0

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

}

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

4

1 回答 1

7

出现问题是因为单元格是可重复使用的,并且您每次都需要在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;
  }

}

于 2013-07-08T22:34:16.703 回答