2015-02-16 2 views
4

В настоящее время я работаю над реализацией inline UIDate picker внутри UITableViewCell.Ошибка приложения при вызове 'tableView endUpdates'

Я могу показать и скрыть эту ячейку выбора, когда я выбираю ячейку непосредственно над тем, где должна быть вставлена ​​эта ячейка, что является поведением, которое я ожидаю. Тем не менее, приложение падает, если выбрать любые другие клетки в виде таблицы:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1582 

Посмотрев на accepted answer to this SO question, я добавил точку останова исключения, и я обнаружил, что приложение рушится при вызове [tableView endUpdates]; в didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    // Check to see if the "Only Alert From" row was selected. The cell with the picker should be below this one. 
    if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow && self.timePickerIsShowing == NO){ 

     [tableView beginUpdates]; 
     [self showTimePicker]; 
     [tableView endUpdates]; 

    } else{ 
     [tableView beginUpdates]; 
     [self hideTimePicker]; 
     [tableView endUpdates]; 
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    } 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

что сказал, я не знаю, как поступить. Если я прокомментирую звонок [tableView endUpdates];, приложение не будет разбиваться, когда будут выбраны другие ячейки, но ячейка с видом подборщика не скроется. У кого-нибудь есть предложения? Спасибо!

EDIT: Вот мой код showTimePicker и hideTimePicker:

- (void)showTimePicker 
{ 
    self.timePickerIsShowing = YES; 
    self.timePicker.hidden = NO; 

    //Create the index path where we insert the cell with the picker 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection]; 

    [self.tableView beginUpdates]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 

    self.timePicker.alpha = 0.0f; 
    [UIView animateWithDuration:0.25 animations:^{ 
     self.timePicker.alpha = 1.0f; 
     //This is the row where the picker cell should be inserted 
     [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection]] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.tableView reloadData]; 
    }]; 
} 

- (void)hideTimePicker { 
    self.timePickerIsShowing = NO; 
    self.timePicker.hidden = YES; 

    //Create the index path where we delete the cell with the picker 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection]; 
    [self.tableView beginUpdates]; 
    //Delete the picker row 
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 
    [UIView animateWithDuration:0.25 
        animations:^{ 
         self.timePicker.alpha = 0.0f; 
        } 
        completion:^(BOOL finished){ 
         self.timePicker.hidden = YES; 
        }]; 
} 

EDIT 2: После прочтения this SO thread, я считаю, что проблема может быть с моим numberOfRowsInSection способом:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch (section) { 
     case NotificationsSection: 
      return TotalPreferencesRows; 
      break; 
     case RedZoneSection: 
      return TotalRedZoneRows; 
      break; 
     case TimeOfDaySection: 
      if (self.timePickerIsShowing) { 
       return TotalTimeOfDayRows + 1; 
      } 
//   else if (self.timePickerIsShowing == NO){ 
//    return TotalTimeOfDayRows; 
//   } 
      else{ 
       return TotalTimeOfDayRows; 
      } 
      return TotalTimeOfDayRows; 
      break; 
     default: 
      return 0; 
      break; 
    } 
} 
+0

Не могли бы вы опубликовать код для методов показать/скрыть TimePicker? Было бы полезно посмотреть, как вы показываете timePicker – Alex

+0

Да, конечно, мои извинения! – narner

+0

Пожалуйста, покажите журнал аварий. Также добавьте контрольную точку исключения, чтобы показать, где она сбой. – Fogmeister

ответ

1

Проблема в вашем didSelectRowAtIndexPath, вы вызываете метод hide, даже если он может не отображаться. Сделать оговорку else в else if, как это:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    // Check to see if the "Only Alert From" row was selected. The cell with the picker should be below this one. 
    if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow && self.timePickerIsShowing == NO){ 

     [tableView beginUpdates]; 
     [self showTimePicker]; 
     [tableView endUpdates]; 

    } else if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow && self.timePickerIsShowing == YES){ 
     [tableView beginUpdates]; 
     [self hideTimePicker]; 
     [tableView endUpdates]; 
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    } 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
+0

Это сработало, спасибо! – narner

2

Не уверен, что если это источник сбоя, но не рекомендуется вызывать beginUpdates несколько раз, что вы делаете в
[tableView beginUpdates]; [self showTimePicker]; [tableView endUpdates]; потому что showTimePicker вызывает [self.tableView beginUpdates];

+0

Спасибо Ким, я только что вытащил вызовы для того, чтобы быть и заканчивать обновления в 'didSelectRowAtIndexPath', но на этот раз произошел сбой при вызове '[tableView endUpdates]; в 'hideTimePicker' – narner

+0

Просто, чтобы быть уверенным: вы вытащили ВСЕ вызовы, чтобы начать/endUpdates в didSelectRowAtIndexPath? Также те, что находятся вокруг '[self hideTimePicker];'? – Alf

+0

Да, я вынул все вызовы, чтобы начать/endUpdates в файле didSelectRowAtIndexPath. Единственные места, где эти вызовы существуют, находятся в моих методах show/hideTimePicker. – narner

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