2013-07-03 28 views
1

我有一個從數據庫成功填充的UITableView,放入一個NSArray中,每個單元格都是可點擊的,並在選中時執行正確的操作,但列表不符合字母順序。爲了將它們整理成字母順序,我使用下面的代碼:UITableView Alphabetizing

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) 

謝謝你之前!

+0

發佈選中單元格時運行的代碼(例如,如果您以這種方式處理您的'didSelectRowAtIndexPath')以及發生異常的代碼行(您可以通過檢查控制檯中的調用堆棧來確定)。 –

+0

我在'[self.navigationController pushViewController:infoInst animated:YES]'行添加了發生異常的'didSelectRowAtIndexPath'代碼。據我所知,「infoInst」不以任何方式爲空,並且索引值應該在適當的範圍內。 – VonKoob

+0

該行不訪問數組。你能從這一行發佈堆棧跟蹤嗎?也許這裏有線索。 –

回答

0

我在didSelectRowAtIndexPath中看到的唯一一個數組訪問是[self.listData objectAtIndex:r]。所以看起來你的listData數組與表的數據模型不同步。從您發佈的代碼中,數組sortedArray應該對應於數據模型,因此請確保listDatasortedArray保持同步或只有一個數組。如果您仍然無法弄清楚,請發佈您的方法以及設置各種數組變量的方法。

+0

你的區域完全正確,我的'listData'數組與'sortedArray'不同步。我試圖找出一種方法,我可以保持兩個同步。非常感謝您的輸入 – VonKoob

+0

@VonKoob我注意到您最初接受了我的答案,然後接受了根據您的意見而不起作用的不同答案。你能澄清嗎? –

+0

對不起,我想我錯誤地意外地點擊了另一個答案複選框。我的問題的根源實際上是同事代碼中的一個錯誤,它導致數組尋找界限之外的索引。謝謝你,對於錯誤選擇你的答案感到抱歉。 – VonKoob

0
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES]; 
sortedArray = [buildings count] <= 1 ? buildings : [buildings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]; 

除非你需要sortedArray是一個不同的實例比buildings。那麼你應該相應地改變陳述。

+0

我根據您的建議更改了我的代碼,但功能沒有變化,錯誤仍然存​​在。關於「建築物」的一些信息是,「建築物」中充滿了具有多個屬性的自定義對象「建築物」,其中一個屬性是數組應按字母順序排列的名稱屬性。 – VonKoob