Objective c UITableView字母排序
我有一个UITableView,它成功地从数据库填充,放入NSArray,每个单元格都可以单击,并在选中时执行正确的操作,但是列表的字母顺序不正确。为了按字母顺序对它们进行排序,我使用以下代码:Objective c UITableView字母排序,objective-c,uitableview,nsarray,alphabetical,Objective C,Uitableview,Nsarray,Alphabetical,我有一个UITableView,它成功地从数据库填充,放入NSArray,每个单元格都可以单击,并在选中时执行正确的操作,但是列表的字母顺序不正确。为了按字母顺序对它们进行排序,我使用以下代码: NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES]; sortedArray = [buildings sortedArrayUsingDescriptors:[NSArray a
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES];
sortedArray = [buildings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
字母排序工作正常,但是当单击某些单元格(在本例中,它是列表中的第一个单元格)时,应用程序终止,出现未捕获的异常:
'NSRangeException' '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
我很乐意发布更多的代码摘录,我只是不确定需要什么相关信息来帮助回答这个问题
编辑:
编辑:这是抛出错误之前的堆栈跟踪
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)
谢谢大家! NSSortDescriptor*sort=[NSSortDescriptor sortDescriptorWithKey:@“Name”升序:YES];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES];
sortedArray = [buildings count] <= 1 ? buildings : [buildings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
sortedArray=[buildings count]我在didSelectRowAtIndexPath
中看到的唯一数组访问是[self.listData objectAtIndex:r]
。因此,您的listData
数组似乎与表的数据模型不同步。根据您发布的代码,数组sortedArray
应与数据模型相对应,因此确保listData
和sortedArray
保持同步或只有一个数组。如果您仍然无法理解,请发布您的UITableViewDataSource
方法以及设置各种数组变量的方法。我根据您的建议更改了代码,但是功能没有更改,错误仍然发生。有关建筑物
的一些信息是,建筑物
填充了一个自定义对象建筑物
,该对象具有多个属性,其中一个属性是数组应按字母顺序排列的名称属性。请发布选定单元格时运行的代码(例如,你的didSelectRowAtIndexPath
如果你这样处理的话)以及发生异常的代码行(你可以通过检查控制台中的调用堆栈来确定)。我在代码行didSelectRowAtIndexPath
中添加了异常发生的代码[self.navigationController pushViewController:infoInst动画:是]
。据我所知,infoInst
在任何方面都不是空的,索引值应该在适当的范围内。那一行不能访问数组。你能从那一行开始发布堆栈跟踪吗?可能有线索。@Timothymose很抱歉我不能使用调试器,但我希望我已经添加了正确的未对问题进行堆栈跟踪。堆栈跟踪在-[Directory tableView:didselectRowAtIndex Path:
”中表示超出界限的数组访问,但上面注释中提到的行无法访问数组。是否可以重复检查哪些代码正在引发异常?如果尚未这样做,请添加异常断点(在断点导航器中,单击“+”,然后单击“添加异常断点…”)。您的区域完全正确,我的列表数据
数组与sortedArray
不同步。我正在尝试找出一种方法,使两者保持同步。非常感谢您的支持input@VonKoob我注意到你最初接受了我的答案,后来又接受了一个不同的答案,但根据你的同事的说法,这个答案并不适用提示。您能澄清一下吗?对不起,我想我只是无意中点击了错误的另一个答案复选框。我的问题的根源实际上是同事代码中的错误,导致数组查找超出范围的索引。谢谢您,并对取消选择您的答案的错误表示抱歉。
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES];
sortedArray = [buildings count] <= 1 ? buildings : [buildings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];