插入UITableView DataSource的字符串未插入

时间:2013-03-23 19:14:54

标签: objective-c nsstring nsarray

每当我从视图中插入NSString返回到UITableView的数据源时,当插入新单元格的调用被放置时,程序崩溃。以下是代码:

- (void)insertTableObject:(NSString *)thing
{
    if (!locations) locations = [[NSMutableArray alloc] init];
    [locations addObject:[NSString stringWithFormat:@"%@", thing]];
    NSLog(@"%@", locations);
    self.tableContents = [[NSMutableDictionary alloc] initWithObjectsAndKeys:locations,@"", nil];
    self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)];
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:([locations count]-1) inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

下面是打印出来的数组,以及随之发生的错误:

2013-03-23 15:06:32.433 XXX[9149:907] (
"D.C",
la
)
2013-03-23 15:06:32.439 XXX[9149:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x320852a3 0x39f1b97f 0x31fd0b75 0xc05bd 0x33ed854d 0x33ec9685 0x33ec5d55 0x33ec4d27 0x33ec1bf9 0x33ff6b27 0xbeceb 0xf6413 0x33f780c5 0x33f78077 0x33f78055 0x33f7790b 0x33f77e01 0x33ea05f1 0x33e8d801 0x33e8d11b 0x35b815a3 0x35b811d3 0x3205a173 0x3205a117 0x32058f99 0x31fcbebd 0x31fcbd49 0x35b802eb 0x33ee1301 0xb701b 0xb6f70)
libc++abi.dylib: terminate called throwing an exception

“la”打印没有引号是一个问题吗?系统可能没有看到该条目吗?

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题。我有细节与细节视图,所以当它绘制detailView索引时,它不存在。我在原始帖子中为函数添加了一个简单的行,以便为detailView数组添加另一个值。