2011-04-16 163 views
0

使用iPhone模擬器2.2.1iPhone應用程序凍結

使用表格並希望將複選標記添加到所選單元格。

我從一個應用程序中添加了這段代碼,該應用程序可以工作到不同的應用程序,但使用同樣的方法。現在,這個新代碼編譯並啓動,並在選定的單元格中打上覆選標記,幾秒鐘後程序凍結。

*此代碼部分來自一個工作應用程序。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

***This snippet was added (from another working app) 

    if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) 
     [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];  
     else 
     [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f]; 

***End of snippet 

} 

我爲控制檯放置了一些printf語句,執行過程通過if語句並通過最後一行。

所以它沒有代碼片段,如果我包含它編譯,啓動的代碼片段,並且在選擇一個項目並且出現複選標記後,應用程序會凍結。

控制檯的錯誤消息如下。

*** -[RootViewController deselect]: unrecognized selector sent to instance 0x526750 
2011-04-16 16:15:30.132 lab3[37268:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[RootViewController deselect]: unrecognized selector sent to instance 0x526750' 
+0

可能重複的[NSInvalidArgumentException](http://stackoverflow.com/questions/726727/nsinvalidargumentexception) – Caleb 2011-04-16 21:20:01

+1

它真的有幫助,如果你實際上讀取錯誤信息說(什麼時候考慮你複製的代碼) 。 – Eiko 2011-04-16 21:33:03

回答

0

你發送一個-deselect消息self,這顯然是RootViewController的實例。但是,您的RootViewController類沒有-deselect方法,因此會引發異常,導致錯誤。因此,要麼停止發送-deselectself,要麼將-deselect方法添加到RootViewController。

順便說一句,搜索NSInvalidArgumentException會出現很多很多類似的問題,因此您不必等待所有答案。

1

答案出現在錯誤消息中:對於對象RootViewController,沒有選擇器deselect,在您的代碼(和代碼段)中代表self。方法deselect必須是在其他程序中定義的方法,但不在當前應用程序中。從其他程序中剪切並粘貼,我敢打賭解決了這個問題。

另一個提示(與問題無關,但代碼如下):如果您在if語句之前聲明局部變量,然後在if語句中使用該變量,則可以避免所有對cellForRowAtIndexPath:的調用。

UITAbleViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
if ([cell accessoryType] == UITableViewCellAccessoryCheckmark) 
    .... 

一旦你解決了其他問題,你的表現會提高。