2011-09-08 13 views
9

Jeśli wywołasz reloadRowsAtIndexPaths dla pierwszej komórki sekcji, z poprzednią sekcją pustą, a ta powyżej niepustą, dostaję dziwny błąd animacji (nawet jeśli podaję "UITableViewRowAnimationNone"), gdzie przeładowana komórka ześlizguje się z powyższej sekcji ..UITableView reloadRowsAtIndexPaths glitch graficzny

starałem się uprościć przykład jak najwięcej:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if (section == 0) 
    return 1; 
else if (section == 1) 
    return 0; 
else if (section == 2) 
    return 3; 
return 0; 
} 

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell... 
cell.textLabel.text = @"Text"; 

return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSArray *editedCell = [[NSArray alloc] initWithObjects:indexPath, nil]; 
//[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:editedCell withRowAnimation:UITableViewRowAnimationNone]; 
//[self.tableView endUpdates]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
return @"Section"; 
} 

Właściwie można Wykomentuj Ostatnia metoda, ale daje lepsze zrozumienie problemu.

Odpowiedz

12

Możesz ustawić wartości bezpośrednio w komórce, nie pozwalając stołowi na ponowne załadowanie się (i tym samym uniknięcie niechcianych animacji). Również, aby kod wyraźniejszy i uniknąć powielania kodu pozwala przenieść konfigurację komórek do osobnej metody (tak będziemy mogli nazwać to z różnych miejsc):

- (void) setupCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath { 
    cell.textLabel.text = @"Text"; // Or any value depending on index path 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [self setupCell:cell forIndexPath:indexPath]; 
} 

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

    // Configure the cell... 
    [self setupCell:cell forIndexPath:indexPath]; 

    return cell; 
} 
+0

To świetne rozwiązanie, tak, jest dziwne zachowanie od reloadRowsAtIndexPaths błąd, lub po prostu używałem go w niewłaściwy sposób? – Fr4ncis

+0

@ Fr4ncis, nie jestem pewien. Aby ponownie załadować widok tabeli komórek, może być konieczne dodanie/usunięcie ich z hierarchii widoków, przebudowanie niektórych jej podekspozycji lub czegoś innego - to zależy od tego, jak wszystkie te transformacje są zaimplementowane wewnętrznie – Vladimir

+0

dziękuję, twoje rozwiązanie jest czyste i dobrze zorganizowane. – jalopezsuarez

Powiązane problemy