2011-02-01 79 views
0

我有一个问题,我看到很多人......但是没有一个修复程序正在为我工​​作。但是当我将NSLog命令放入代码时,我注意到发生了一些奇怪的事情。删除UITableView行问题

我有标准删除方法:

- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.recipes removeObjectAtIndex: indexPath.row]; 
     [tableView beginUpdates]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView endUpdates]; 

    } 

} 

食谱是保持数据源的阵列。

理论上这应该工作得很好,但我得到的错误:

invalid number of rows in section 1. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted). 

但我知道,这是分崩离析,当我添加的NSLog到numberOfRowsInSection,我看到,该方法被调用两次从上面的方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    NSLog(@"THIS CALLED TWICE %i",[recipes count]); 
    return [recipes count]; 
} 

任何人都知道还有什么可能导致numberOfRowsInSection方法触发两次?

谢谢你的时间。

+0

删除标记'xcode'http://stackoverflow.com/tags/xcode/info – vikingosegundo 2011-02-14 18:20:05

回答

0

写这之后,我看到了,我有以下几点:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 

显然,numberOfRowsInSection被每节...更改代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     // Return the number of sections. 
     return 1; 
    } 

固定的问题。

+0

然后勾选您的答案! – Sarah 2011-02-02 07:14:15