2015-02-05 20 views
0

嘗試使用可重用單元從表格視圖中刪除項目的IOS newb。我怎麼知道我需要刪除的項目以及如何刪除它。數據來自網絡調用,單元有自己的xib/class。我在單元類上放置了一個屬性,以便能夠獲取他們的數據。這是我目前的代碼。它試圖從數據提供程序中刪除項目時崩潰。從uitableview的數據提供程序中刪除項目

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 
if(editingStyle == UITableViewCellEditingStyleDelete){ 
    //find object to be deleted 
    MessagesCell *curCell = (MessagesCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    for (int i=0;i<[messagesArray count]; i++) { 
     if ([[CommonAPI checkForNullNumber:curCell.allData[@"id"]] integerValue] ==[[CommonAPI checkForNullNumber:((NSDictionary *)[messagesArray objectAtIndex:i])[@"id"]] integerValue]) { 
      [messagesArray removeObjectAtIndex:i]; 
     } 
    } 
} 
[messagesTable reloadData]; 
} 

品進入刪除呼叫,所以我懷疑它的,即使必須有查找數據從dataProvider中細胞的一種更好的方式我的循環。無論如何感謝提前。

+0

當應用程序崩潰時,您會看到什麼樣的錯誤? – Stonz2 2015-02-05 20:00:15

回答

0

首先,你必須獲得對象中刪除和更新

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     MyClass * object = [messagesArray objectAtIndex:indexPath.row]; 
     [messagesArray removeObject:object]; 
     [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 
} 
0

紐比的問題。我在網絡調用的響應中實例化了數組字典中的nsMutableArray。所以我正在將nsmutable數組實例化爲一個N​​SArray。當你嘗試改變它中的任何元素時它就會死亡。所以要解決這個問題,我正確地實例化了這個數組。

所以不正確的方法是

messagesArray = response[@"data"]; 

正確的方式

messagesArray = [[NSMutableArray alloc] initWithArray:response[@"data"]]; 

感謝您的幫助。

相關問題