0

こんにちは、私はカスタム テーブルを作成し、テーブルに必要なデリゲートとデータソースをすべて追加しました。これはうまく機能していますが、テーブルをスクロールすると、このようにコンテンツが変更されてコードになります...

Table_worklist=[[UITableView alloc]initWithFrame:CGRectMake(10,480,750,130)style:UITableViewStylePlain];
Table_worklist.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Table_worklist.delegate=self;
Table_worklist.dataSource=self;
Table_worklist.layer.borderWidth = 2.0;
Table_worklist.layer.borderColor = [UIColor grayColor].CGColor;
[ScrollView addSubview:Table_worklist];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell== nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

     UITextField  *resultval=[[UITextField alloc]initWithFrame:CGRectMake(510,10,120,30)];
        resultval.tag=1;
        resultval.font = [UIFont boldSystemFontOfSize:12.0];
        //the horizontal alignment of the text
        resultval.textAlignment = NSTextAlignmentCenter;
    resultval.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
        resultval.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
        resultval.delegate =self;
    if([[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Numerical"])
  {
    imageLayer = field.layer;
    [imageLayer setCornerRadius:02];
    [imageLayer setBorderWidth:1];
    imageLayer.borderColor=[[UIColor lightGrayColor]CGColor];

   }
    else if([[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Words"]||[[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Comment"])
{
    imageLayer = field.layer;
    [imageLayer setCornerRadius:06];
    [imageLayer setBorderWidth:1];
    imageLayer.borderColor=[[UIColor blackColor]CGColor];
   UIButton *CheckMark=[[UIButton alloc]initWithFrame:CGRectMake(540,10,30,30)];
    CheckMark.tag=1;
    [CheckMark setImage:[UIImage imageNamed:@"DownTriangle1"]forState:UIControlStateNormal];

    imageLayer = CheckMark.layer;
    [imageLayer setCornerRadius:02];
    [imageLayer setBorderWidth:1];
    imageLayer.borderColor=[[UIColor blackColor] CGColor];
    [cell.contentView addSubview:CheckMark];

  }

        [cell.contentView addSubview:resultval];

  }

   return cell;
 }

これは、最初の 3 または 4 セルの正しいデータを表示します (テーブルの高さが 130 であるため、一度に 3 つのセルしか表示されないため)。

   if([[NSString stringWithFormat:@"%@",[[TestSample objectAtIndex:indexPath.row]valueForKey:@"ResultType"]]isEqualToString:@"Numerical"])
   {     }

次に、データは数値です go this if condition otherwise go else condition ......しかし、7-8セルのようなセルがさらにある場合、これらのセルをスクロールすると、セル4でコントローラーが他に移動すると仮定しますif 条件のコメントと単語データ .........最後にセル -8 が数値データを取得したため、コントローラーが if 条件に入りますが、if 条件データに表示される if else 条件で作成されたチェック マーク ボタン .. ....ですから、条件を変更する方法をスクロールしたときに何が起こったのかわかりません ...... -8........しかし、コントローラーはセル8のif条件で適切な場所に移動します....しかし、前回はセル8で生成されたチェックマークボタンを作成しました...方法この問題を解決します....

4

1 に答える 1

1

私の回答に従う前に、次のコードは の行ごとに新しいセルを作成するため、メモリ管理に悪いことを伝えたいUITableViewので、注意してください。

ただし、UITableView行が制限されている場合(約50〜100行)、次のコードが役立つ場合は、それを使用することをお勧めします。

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         /// Put your code here
     }

    return cell;
}

行が限られている場合は、これが最適なコードです。

于 2013-08-03T06:47:21.897 に答える