2016-11-03 5 views
1

Когда я просматриваю таблицу, галочка скрывается. Я знаю из-за повторного использования Cell, но я не знаю, как это исправить. Помогите мне. Вот мой код:Контрольная метка в таблицеViewCell скрыта при прокрутке

- (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 = ДА ?? –

+0

Мое мнение выбрали btn. Nomorl: кнопка Cancel is hidde, а ячейка - деталь. Когда я нажму на него, просмотрите кнопку showCancel, а ячейка - AccessoryNone. _btnCancel.hidden = YES, чтобы проверить, чтобы показать деталь ячейки или выбрать ячейку. – Rin

ответ

2

Когда вы прокручиваете представление таблицы, будет вызываться cellForRowAtIndexPath для конкретной ячейки, в которой вы устанавливаете accessoryType как None. Вместо этого попробуйте, как показано ниже.

- (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

Попробуйте это:

Вам нужно добавить экземпляр объекта TeacherInfo вместо студенческого билета, потому что ваш containedObject массива дает неверный результат.

- (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 и истинна ложь на основе выбора таблицы строк.

Смежные вопросы