2011-02-01 77 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