UITableView reloadRowsAtIndexPaths графический глюк - PullRequest
9 голосов
/ 08 сентября 2011

Если я вызываю reloadRowsAtIndexPaths для первой ячейки раздела, с предыдущим разделом пустым, а предыдущий - не пустым, я получаю странный глюк анимации (даже если я указываю «UITableViewRowAnimationNone»), когда перезагруженная ячейка скользит вниз от выше раздел ..

Я попытался максимально упростить пример:

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

На самом деле вы можете закомментировать последний метод, но он дает лучшее понимание проблемы.

1 Ответ

13 голосов
/ 08 сентября 2011

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

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