iOS UITableView行在reloadSections上消失:withRowAnimation

时间:2013-07-28 22:20:48

标签: ios uitableview animation tableview reload

我有一个使用storyboard设计的静态tableview。每当我选择一个单元格并调用reloadSections:withRowAnimation:它会导致它上面的两个单元格消失但显示它应该的4个单元格。有人知道为什么会这样吗?

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

{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

if (indexPath.section == 1) {
        if (indexPath.row == 0) {

        }
        else if (indexPath.row == 1) { // Map Type Cell

            self.isSelectingMapType = ![self isSelectingMapType];
            [self.tableView beginUpdates];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
            [self.tableView endUpdates];
        }
        else {

            // Configure the single selection
            if (self.checkedIndexPath) {
                UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
                uncheckCell.accessoryType = UITableViewCellAccessoryNone;
            }

            // Check the cell
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;

            // Store the new indexPath
            self.checkedIndexPath = indexPath;

            // Save the map type indexPath
            [LBSettings saveObject:indexPath forKey:kLBSettingsMapTypeIndexPath];

            // Save the map type
            if (indexPath.row == 2) {

                // Save the map type standard
                [LBSettings saveObject:[NSNumber numberWithInt:kGMSTypeNormal] forKey:kLBSettingsMapType];
            }
            if (indexPath.row == 3) {

                // Save the map type satellite
                [LBSettings saveObject:[NSNumber numberWithInt:kGMSTypeSatellite] forKey:kLBSettingsMapType];
            }
            if (indexPath.row == 4) {

                // Save the map type hybrid
                [LBSettings saveObject:[NSNumber numberWithInt:kGMSTypeHybrid] forKey:kLBSettingsMapType];
            }
            if (indexPath.row == 5) {

                // Save the map type terrian
                [LBSettings saveObject:[NSNumber numberWithInt:kGMSTypeTerrain] forKey:kLBSettingsMapType];
            }

            self.isSelectingMapType = ![self isSelectingMapType];
            [self.tableView beginUpdates];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
            [self.tableView endUpdates];
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in each section
    switch (section) {
        case 0:
            return 3;
            break;
        case 1:
            if (self.isSelectingMapType == YES) {
                return 6;
            }
            return 2;
            break;
        case 2:
            return 2;
            break;
        case 3:
            return 6;
            break;
        case 4:
            return 0;
            break;
        default:
            break;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

尝试在动画事务完成后调用reloadData。

[CATransaction begin]; 

[CATransaction setCompletionBlock:^{ 
     [self.tableView reloadData];
}];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];

[CATransaction commit];
相关问题