2011-09-08 79 views
9

如果我爲段的第一個單元調用reloadRowsAtIndexPaths,前面的段爲空,而上面的段不爲空,我得到一個奇怪的動畫故障(即使我指定「UITableViewRowAnimationNone」)重載單元從幻燈片上一節下來..UITableView reloadRowsAtIndexPaths圖形故障

我試圖簡化儘可能的例子:

- (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"; 
} 

其實你可以註釋掉的最後一個方法,但它提供了一個更好的理解這個問題。

回答

12

您可以直接設置想要的單元格的值,而不是讓表重新加載自身(從而避免任何不需要的動畫)。也使代碼更清晰,避免重複代碼可以讓移動小區建立一個單獨的方法(這樣我們就可以從不同的地點調用它):

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

這是一個很好的解決方案,反正是從奇怪的行爲reloadRowsAtIndexPaths一個錯誤,或者我只是以錯誤的方式使用它? – Fr4ncis

+0

@ Fr4ncis,我不確定。重新加載單元格表格視圖可能需要將它們從視圖層次結構中添加/刪除,重建其子視圖或其他內容 - 這取決於所有這些轉換是如何在內部實現的 – Vladimir

+0

謝謝,您的解決方案乾淨且結構良好。 – jalopezsuarez