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 に答える