2014-01-24 91 views
-1

我創建了一個鬧鐘應用程序,在主屏幕tableview同時向左滑動行必須被刪除,我使用下面的代碼,但是應用程序崩潰引發異常錯誤當刪除UITableView行時出現NSRangeException行

-(void)tableView:(UITableView *)tableView commitEditingStyle: 

(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     [self.loadRemainderArray removeObjectAtIndex:indexPath.row]; 
    } 
} 

我得到的錯誤是:終止應用程序由於未捕獲的異常「NSRangeException」,原因:。-[NSMutableArray removeObjectAtIndex:]: index 0 beyond bounds for empty array

+1

這是因爲如錯誤所述,你的'loadRemainderArray'是空的,你試圖從它中刪除一個對象。 – Zen

回答

0

的原因是你所得到的錯誤是因爲你的數組爲空它可能的原因空是:

  1. 你沒有初始化數組:

    self.loadRemainderArray = [[NSMutableArray alloc]init] 
    
  2. 你永遠不添加一個對象到你的數組:

    [self.loadRemainderArray addObject:yourObject]; 
    
0

數組你想從刪除對象爲空或未初始化。 - 你需要找到問題的根源理解爲什麼數組爲空或零

0

此行導致您的崩潰

if (indexPath.row < [self.loadRemainderArray count]) 
[self.loadRemainderArray removeObjectAtIndex:indexPath.row]; 

,這並不會解決你的問題:應該按如下步驟來保護代碼。 [self.loadRemainderArray removeObjectAtIndex:indexPath.row];。因爲你嘗試從空數組中刪除對象。只需編寫此代碼即可。

if (self.loadRemainderArray.count > indexPath.row) 
    [self.loadRemainderArray removeObjectAtIndex:indexPath.row]; 

我想,你在其他地方做錯了,所以在這個方法中數組是空的。請在代碼中檢查loadRemainderArray的訪問權限。

0

可能有以下兩個原因之一,你得到這個例外。

  1. 你忘了[NSMutableArray alloc] init]你的數組。沒有初始化添加對象。 self.loadRemainderArray = [NSMutableArray alloc] init];

  2. 您尚未向此陣列添加任何對象。

+0

當這個'commitEditingStyle:'委託會調用?你怎麼說他沒有添加對象數組?你的回答和Abdullah Shafique的回答有什麼區別? – Mani

+0

我剛剛給第一點加了一點說明。我只是告訴了這個例外的可能性。 – Palak

+0

這一點你有沒有告訴過'沒有初始化添加的對象?現在告訴你,你增加了什麼作爲額外的點? – Mani