2012-01-09 66 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时重新加载表格视图。

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

相关问题