2012-01-09 65 views
0

當我滾動我的UITableView下來,然後向上滾動時,應用程序崩潰與下面的堆棧:程序接收到的信號SIGABRT向上滾動的UITableView

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 48]' 

我知道這是說,我正在訪問超過小區索引UITableView的大小,但我無法弄清楚如何解決它。這可能是我的相關代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"CheckedTableViewCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]];//this line may be the source of the crash 
    cell.textLabel.text = [rowData objectForKey:kCellTextKey]; 

    if ([[rowData objectForKey:kCellStateKey] boolValue]) { 
     UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]]; 
     cell.accessoryView = imageView1;  
    } else { 
     UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]]; 
     cell.accessoryView = imageView2; 
    } 

    return cell; 
    } 

編輯

這裏是我的tableIndexFromIndexPath方法實現

- (NSUInteger)tableIndexFromIndexPath:(NSIndexPath *)indexPath { 
    // Get list of items at selected section 
    NSArray *listData = [tableContents objectForKey:[sortedKeys objectAtIndex:indexPath.section]]; 
    // Get name of selected row within the section 
    NSString *textKey = [listData objectAtIndex:indexPath.row]; 
    // Look up that name in the tableData array 
    for (int i=0; i < [tableData count]; i++) { 
     NSDictionary *dict = [tableData objectAtIndex:i]; 
     if ([[dict objectForKey:kCellTextKey] isEqualToString:textKey]) { 
      return i; 
     } 
    } 
    //In case Name was not found 
    return NSNotFound; 
} 
+1

你的tableIndexFromIndexPath方法做了什麼? – Vladimir 2012-01-09 14:40:51

+0

@Malek - 同意代碼中的註釋,'tableIndexFromIndexPath'看起來像什麼? – 2012-01-09 14:41:09

+0

我已經編輯了我的帖子,實際上,在'tableIndexFromIndexPath'方法中,我考慮到從我的UITableView中找不到名稱並且返回'NSNotFound'的情況。 – Malloc 2012-01-09 15:05:08

回答

1

變化

NSDictionary *rowData = [self.tableData objectAtIndex:[self tableIndexFromIndexPath:indexPath]]; 

NSDictionary *rowData = [self.tableData objectAtIndex:indexPath.row]; 
+0

你好'blake305',實際上我不能改變它,因爲我使用了兩個可變數組(我的UITableView分成了多個部分),否則當我選擇一個單元我的一個部分,它會隨機檢查其他部分的其他細胞,請看看我的更新:) – Malloc 2012-01-09 15:25:16

0

正如一些人指出的那樣,您的tableIndexFromIndexPath:不正確。具體來說,它返回NSNotFound,這表明您使用類似於indexOfObject:的東西來處理不在數組中的對象。

0
  1. 首先檢查您是否獲得了所有NSMutableArray的價值?
  2. 如果你正在檢查你的tableView的段數是否等於NSMutableArray數組的數量還是沒有?
  3. 如果相同,則會出現重新加載問題,然後在將數據插入NSMutableArray時重新加載表格視圖。

我希望你能找到你的解決方案。

相關問題