无效更新:部分中的行数无效。无法为TableView

时间:2016-04-05 16:59:06

标签: ios objective-c uitableview

错误讯息 -

无效更新:第2节中的行数无效。更新后的现有部分中包含的行数(8)必须等于更新前该部分中包含的行数(8),或者减去从该部分插入或删除的行数(插入1个,删除0个),加上或减去移入或移出该部分的行数(0移入,0移出)。'

尝试在特定部分和行中点击时添加自定义UITableViewCell。类型是我在cellForRowAtIndexPath中定义的枚举 -

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

BucketListCellType type = [[[self.presentedCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]  intValue];

  if (type == mainPhotoCellWithAddToBucketList) {

    BucketListShellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

else if (type == mainPhotoCell) {

    BucketListShellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
else if (type == CreateNewBucketListCell) {
    CreateNewBucketListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Create" forIndexPath:indexPath];
//        self.collectionView.alpha = 0;
    return cell;

}

else if (type == CategoryCell) {

    BucketListCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Category" forIndexPath:indexPath];
    BucketListCategory *category = [self.bucketListCategories objectAtIndex:indexPath.row - 1];
    cell.bucketListNameLabel.text = category.name;

    return cell;

}

else if (type == CategoryPhotosCell) {
    BucketListCategoryPhotosTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CategoryPhotos"];
    return cell;

}
return nil
}

以下是点击单元格时的代码 -

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

BucketListCellType type = [[[self.presentedCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] intValue];


if (type == CreateNewBucketListCell) {


    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"New Bucket List"
                                  message:@"Create a new Bucket List"
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   //Do Some action here

                                                   [APIManager createBucketListCategory:alert.textFields.firstObject.text completionHandler:^(RequestStatus status) {

                                                   }];
                                               }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       [alert dismissViewControllerAnimated:YES completion:nil];
                                                   }];

    [alert addAction:ok];
    [alert addAction:cancel];

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"I want to..";
    }];

    [self presentViewController:alert animated:YES completion:nil];
}
else if (type == CategoryCell) {


    [self expandCategoryCell];

}

}

这是下一个方法

-(void)expandCategoryCell {
NSMutableArray *preCells = [NSMutableArray new];

// First Section - Suggested Bucket List Images

NSMutableArray *suggestedBucketListImages = [NSMutableArray new];

[suggestedBucketListImages addObject:@(mainPhotoCellWithAddToBucketList)];
[suggestedBucketListImages addObject:@(mainPhotoCellWithAddToBucketList)];

[preCells addObject:[NSArray arrayWithArray:suggestedBucketListImages]];

// Second Section - Top Bucket List Images

NSMutableArray *topBucketListImages = [NSMutableArray new];

[topBucketListImages addObject:@(mainPhotoCell)];

[preCells addObject:[NSArray arrayWithArray:topBucketListImages]];

// Third Section - My Bucket List Images

NSMutableArray *myBucketListCategories = [NSMutableArray new];

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[myBucketListCategories addObject:@(CreateNewBucketListCell)];

for (int i = 0; i < [self.bucketListCategories count]; i++) {

    [myBucketListCategories addObject:@(CategoryCell)];
    if (i == indexPath.row) {

        [myBucketListCategories addObject:@(CategoryPhotosCell)];

    }


}

[preCells addObject:[NSArray arrayWithArray:myBucketListCategories]];


NSMutableArray *unorganizedBucketListImages = [NSMutableArray new];

[unorganizedBucketListImages addObject:@(mainPhotoCell)];
[preCells addObject:[NSArray arrayWithArray:unorganizedBucketListImages]];

[self.tableView beginUpdates];

self.presentedCells = preCells;
NSArray *indexPaths = @[[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section]];

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

[self.tableView endUpdates];



}

我没有收到任何错误消息。它只是停止在[self.tableView endUpdates]工作。有想法该怎么解决这个吗?谢谢。

0 个答案:

没有答案