2016-11-03 4 views
1

スクロールテーブルの場合、チェックマークはhideです。私はReusing Cellのために知っているが、私は修正する方法を知らない。 Plsは私を助ける。ここに私のコードは次のとおりです。スクロール時にtableViewCellのチェックマークが隠されています

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

    StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[StudentTableViewCell alloc] init]; 
    } 

    if (_btnCancel.hidden == NO) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } else { 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    return cell; 
} 

私がチェックしdidSelectRowAtIndexPathにオフに変更します。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 

    if (_btnCancel.hidden == NO) { 
     if (cellCheck.accessoryType == UITableViewCellAccessoryNone) { 
      cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; 
      TeacherInfo *courseStudent = studentQuitArray[indexPath.row]; 
      [dict setObject:courseStudent.id_user forKey:@"student_id"]; 

      [studentDetail addObject:dict]; 

     } else { 
      cellCheck.accessoryType = UITableViewCellAccessoryNone; 
      [studentDetail removeObject: studentQuitArray[indexPath.row]]; 
     } 

    } 
} 
+2

_btnCancel.hidden = YESの場合はどこですか? –

+0

私の意見はbtnを選択しました。 Nomorl:ボタンキャンセルはhiddeで、セルは詳細です。私がそれをクリックすると、show buttonCancelを表示し、cellはAccessoryNoneです。 _btnCancel.hidden = YESセルの詳細を表示するかセルを選択するかをチェックするだけです。 – Rin

答えて

2

テーブルビューをスクロールすると、accessoryTypeをNoneに設定している特定のセルのcellForRowAtIndexPathが呼び出されます。代わりに以下のようにしてください。

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

    StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[StudentTableViewCell alloc] init]; 
    } 

    if (_btnCancel.hidden == NO) { 
     TeacherInfo *courseStudent = studentQuitArray[indexPath.row]; 
     if ([studentDetail containsObject:courseStudent]) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     else { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    return cell; 
} 

didSelectRowAtIndexPath:これは役立つ

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 

    if (_btnCancel.hidden == NO) { 
     if (cellCheck.accessoryType == UITableViewCellAccessoryNone) { 
      cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; 
      TeacherInfo *courseStudent = studentQuitArray[indexPath.row]; 
      [dict setObject:courseStudent.id_user forKey:@"student_id"]; 

      [studentDetail addObject:dict]; 

     } else { 
      cellCheck.accessoryType = UITableViewCellAccessoryNone; 
      [studentDetail removeObject: studentQuitArray[indexPath.row]]; 
     } 

    } 
} 

希望。

+0

ありがとうございます! – Rin

1

あなたはセルを再利用しているなら、あなたは、各セル.Becauseの状態にあなたがスクロールアップするたびに保存する必要があるとTableViewは、画面外の前のセルを戻します。

選択可能な状態を配列に保存し、cellForRowAtIndexpathでそのインデックスを読み取って、セルの現在の状態を取得することができます。

+0

素晴らしいアイデア!それは仕事です。ありがとうございます、それは仕事です! – Rin

0

はこれを試してみてください:

あなたは、配列のあなたのcontainedObjectが間違った結果を与えるのでTeacherInfoのインスタンスオブジェクトの代わりに、学生のIDを追加する必要があります。

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

     StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
     if (cell == nil) { 
      cell = [[StudentTableViewCell alloc] init]; 
     } 

     if (_btnCancel.hidden == NO) { 
      TeacherInfo *courseStudent = studentQuitArray[indexPath.row]; 
      if ([studentDetail containsObject:courseStudent]) { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      else { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } 
     } else { 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 

     return cell; 
    } 

didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 

    if (_btnCancel.hidden == NO) { 
     if (cellCheck.accessoryType == UITableViewCellAccessoryNone) { 
      cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; 
      TeacherInfo *courseStudent = studentQuitArray[indexPath.row]; 
      [studentDetail addObject:courseStudent]; 

     } else { 
      cellCheck.accessoryType = UITableViewCellAccessoryNone; 
      [studentDetail removeObject: studentQuitArray[indexPath.row]]; 
     } 

    } 
} 
1

また、あなたの「TeacherInfo」NSObjectクラスでは、選択され、テーブル行の選択に基づいて真偽を設定するように1つのブール型プロパティを追加することによってこれを行うことができます。

関連する問題