1

我有一个从数据库成功填充的 UITableView,放入 NSArray 并且每个单元格都是可点击的,并且在选择时执行正确的操作,但是列表不按字母顺序排列。为了将它们按字母顺序排序,我使用以下代码:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES];
sortedArray = [buildings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

字母排序工作正常,但是当单击某些单元格(在此示例中是列表中的第一个单元格)时,应用程序会因未捕获的异常而终止:

'NSRangeException' '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

我很乐意发布更多代码摘录,我只是不确定需要哪些相关信息来帮助回答这个问题。

编辑:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger r = [indexPath row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if(infoInst != nil) {
        infoInst = nil;
    }
    infoInst = [[DirInfoListing alloc] initWithNibName:@"DirInfoListing" bundle:nil building:[self.listData objectAtIndex:r] map:mapUI];

    NSLog(@"infoInst building: %@", [self.listData objectAtIndex:r]);

    NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType isEqualToString:@"iPad"] || [deviceType isEqualToString:@"iPad Simulator"]){
        [self presentModalViewController:infoInst animated:YES];
    } else {
        [self.navigationController pushViewController:infoInst animated:YES];
    }
}

编辑:这是引发错误之前的堆栈跟踪

2013-07-03 16:56:56.579 thestanforddaily[32907:1a303] Stack trace : (
    0   thestanforddaily                    0x002190d9 -[Directory     tableView:didSelectRowAtIndexPath:] + 457
    1   UIKit                               0x01ae871d -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1164
    2   UIKit                               0x01ae8952 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 201
    3   Foundation                          0x0237086d __NSFireDelayedPerform + 389
    4   CoreFoundation                      0x02acd966 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
    5   CoreFoundation                      0x02acd407 __CFRunLoopDoTimer + 551
    6   CoreFoundation                      0x02a307c0 __CFRunLoopRun + 1888
    7   CoreFoundation                      0x02a2fdb4 CFRunLoopRunSpecific + 212
    8   CoreFoundation                      0x02a2fccb CFRunLoopRunInMode + 123
    9   GraphicsServices                    0x0327b879 GSEventRunModal + 207
    10  GraphicsServices                    0x0327b93e GSEventRun + 114
    11  UIKit                               0x01a58a9b UIApplicationMain + 1175
    12  thestanforddaily                    0x00002f4d main + 141
    13  thestanforddaily                    0x00002e75 start + 53
) 
2013-07-03 16:57:13.740 thestanforddaily[32907:1a303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x2af9052 0x28e8d0a 0x2ae5db8 0x21cb0e 0x1b1e64e 0x1b1e941 0x1b3047d 0x1b3066f   0x1b3093b 0x1b313df 0x1b31986 0x1b315a4 0x219250 0x1ae871d 0x1ae8952 0x237086d 0x2acd966 0x2acd407 0x2a307c0 0x2a2fdb4 0x2a2fccb 0x327b879 0x327b93e 0x1a58a9b 0x2f4d 0x2e75)

谢谢!

4

2 回答 2

0

我看到的唯一数组访问didSelectRowAtIndexPath[self.listData objectAtIndex:r]. 因此,您的数组似乎listData与表的数据模型不同步。从您发布的代码中,该数组sortedArray应对应于数据模型,因此请确保listDatasortedArray保持同步或只有一个数组。如果您仍然无法弄清楚,请发布您的UITableViewDataSource方法以及设置各种数组变量的方法。

于 2013-07-03T23:55:49.537 回答
0
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES];
sortedArray = [buildings count] <= 1 ? buildings : [buildings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

除非您需要sortedArraybuildings. 那么您应该相应地更改该语句。

于 2013-07-03T19:25:04.690 回答