开发者

Having problem in deleting the cell

When i try to delete the cell i get crash ...

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2011-07-19 08:55:53.575 MyTable[477:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'

Below is my code

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...
    cell.textLabel.text = [aContentArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text =[aDetailTextArray objectAtIndex:indexPath.row];  
    cell.imageView.image = [UIImage imageNamed:@"home-picture.jpg"];

    return cell;
}
  • (void)viewDidLoad { [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. self.navigationItem.rightBarButtonItem = self.editButtonItem;

    aContentArray = [[NSMutableArray arrayWithObjects:@"iPhone",@"Android",@"Blackberry",@"Symbian",nil]retain];

    aDetailTextArray = [[NSMutableArray arrayWithObjects:@"iPhone is from Apple Inc", @"Android is from Google and it is Open source",@"Blackberry is from RIM Research In Motion",@"Symbian is used from Nokia",nil]retain];

}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [self.aDetailTextArray removeObjectAtIndex:indexPath.row];
        [self.aContentArray removeObjectAtIndex:indexPath.row];

        [tableView beginUpdates];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];

        [tableView reloadData];

    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the 开发者_StackOverflow中文版table view.
    }   
}

Please help me out and thanks


The problem is that you delete the row but you do not remove that data from your datasource (read the error message again and you'll see what I mean). Without seeing how your datasource is implemented it is hard to give an exact answer, but as a demonstration let's say you keep your tableview contents in an NSMutableArray called items.

[tableView beginUpdates];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[items removeObjectAtIndex:indexpath.row]:
[tableView endUpdates];
//no need to call reloaddata

Again, the exact method will depend on how your tableview is populated. The above is only an example of one particular case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜