2010-08-16 20 views
5

Mam UITableView i zasadniczo robię niektóre ustawienia aplikacji, a jeśli pierwsza sekcja UISegmentedControl jest przełączana na indeks 1, to chcę wyświetlić nową sekcję, ale jeśli indeks 1 był wcześniej ustawiony, a użytkownik wybiera indeks 0 wtedy muszę usunąć sekcja 2.UITableView dodawanie/usuwanie sekcji, gdy nie jest w trybie edycji?

aby to zrobić, miałem ten kod ustawiony na ogień na UISegmentedControl's valueChanged event

if (segmentControl.selectedSegmentIndex == 0) 
{ 
    self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) { 

     NSArray *indexSections = [NSArray arrayWithObjects: 
      [NSIndexPath indexPathForRow:0 inSection: 
       [sections indexOfObject:FT_AND_IN]], 
      [NSIndexPath indexPathForRow:0 inSection: 
       [sections indexOfObject:FRACTION_PRECISION]], nil]; 
     [sections removeObject:FT_AND_IN]; 
     [sections removeObject:FRACTION_PRECISION]; 
     [self.tableView deleteRowsAtIndexPaths:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else { 
    self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
    [sections insertObject:FRACTION_PRECISION atIndex:2]; 
    NSArray *indexSections = [NSArray arrayWithObjects: 
     [NSIndexPath indexPathForRow:0 inSection: 
      [sections indexOfObject:FT_AND_IN]], 
     [NSIndexPath indexPathForRow:0 inSection: 
      [sections indexOfObject:FRACTION_PRECISION]], nil]; 
    [self.tableView insertRowsAtIndexPaths:indexSections 
     withRowAnimation:UITableViewRowAnimationRight]; 
} 

Jeżeli NSMutableArray nazywa sections lista wszystkich odcinków. Każda sekcja ma tylko jeden wiersz, więc nie są potrzebne żadne podrzędne tablice.

Jednak przy ocenie część innego otrzymuję ten błąd:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], 
    /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:904 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
    reason: 'Invalid update: invalid number of sections. The number of sections 
    contained in the table view after the update (6) must be equal to the number of 
    sections contained in the table view before the update (4), plus or minus the 
    number of sections inserted or deleted (0 inserted, 0 deleted).' 

I zostały zweryfikowane, że miał 4 sekcje przed else, to poprawnie dodane dwie sekcje do tablicy sections, powiedziałem jej odpowiednie indeksy do dodanych sekcji. Dlaczego to nie działa?

Próbowałem zastąpić linię [self.tableView insertRows/deleteRows...] z [self.tableView reloadData];, a następnie działa dobrze, ale chcę animować dodawanie/usuwanie tych sekcji.

Aktualizacja Próbowałem tę sugestię i prace dodawanie ale jestem coraz upaść na usunięcie

[self.tableView beginUpdates]; 
if (segmentControl.selectedSegmentIndex == 0) 
{ 
     self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && 
      [sections containsObject:FRACTION_PRECISION]) 
     { 

     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: 
       [sections indexOfObject:FT_AND_IN]] 
       withRowAnimation:UITableViewRowAnimationRight]; 
     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: 
       [sections indexOfObject:FRACTION_PRECISION]] 
       withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else 
    { 
     self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
     [sections insertObject:FRACTION_PRECISION atIndex:2]; 
     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1]; 
    NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndex:2]; 
    [self.tableView insertSections:indexSet 
      withRowAnimation:UITableViewRowAnimationRight]; 
    [self.tableView insertSections:indexSet2 
      withRowAnimation:UITableViewRowAnimationRight]; 
} 
[self.tableView endUpdates]; 

otrzymuję ten błąd.

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** - 
    [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds 
    maximum index value of NSNotFound - 1' 

W FT_AND_IN i FRACTION_PRECISION obiekty są tylko dodany/usunięty z magazynu danych w tym kodzie, a oni są tylko const NSString obiekty.

+0

Jest smutny, aby zobaczyć osoby z wysokim wynikiem nie formatować kod – vodkhang

+0

Przypuszcza się, aby działać poprawnie. Nie widzę żadnych problemów. Czy możesz dwukrotnie sprawdzić tablicę indexSections? – vodkhang

Odpowiedz

4

Bardzo trudno odczytać tam niesformatowany kod.

Chcesz spojrzeć na -[UITableView insertSections:withRowAnimation:] i -[UITableView deleteSections:withRowAnimation], jak sądzę.

Spróbuj:

if (segmentControl.selectedSegmentIndex == 0) 
{ 
    self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) { 

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]]; 
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]]; 

     [sections removeObject:FT_AND_IN]; 
     [sections removeObject:FRACTION_PRECISION]; 

     [self.tableView deleteSections:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else { 
    self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
    [sections insertObject:FRACTION_PRECISION atIndex:2]; 

    NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]]; 
    [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]]; 

    [self.tableView insertSections:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
} 
+0

Nadal nie jestem pewien, dlaczego edytowana wersja myne nie działała, ale twoja robi. Jedynym problemem z twoim jest wiersz '[self.tableView addSections ...]' powinien być 'insertSections'. Dzięki! – jamone

Powiązane problemy