2012-07-23 39 views
0

所以我有一個list.plist和我所有的物品。它們都是具有Key和Value的字符串。Xcode-TableView PList Items into Section A-Z

我有'課程'聲明爲NSDictionary和'courseKeys'聲明爲NSArray

要加載的所有項目到我以前this code

我的問題是我的表視圖,我怎麼可以把它使每首字母都有其自己的部分,所以我可以字母間輕鬆導航?

我知道我可以設置sectionIndexTitlesForTableView,並且我可以在右側邊欄上顯示字母A-Z,但是如何讓它們導航到正確的字母?

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
NSMutableArray *searchArray = [NSMutableArray arrayWithObjects: 
         @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", 
         @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; 


return searchArray; 
} 
+0

我喜歡searchArray是如何初始化的...... :( – 2012-07-23 04:50:53

+0

不確定你的意思,我對Objective-C非常陌生並且坦率地說不太瞭解 – 2012-07-23 06:27:11

回答

0

您應該實現3個表視圖委託來使其工作。我會嘗試寫一個半僞碼給你,讓它簡單:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [searchArray count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSArray *courses = [self coursesInSection:section]; 
    return [courses count]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

     NSArray *courses = [self coursesInSection:indexPath.section]; 
     NSString *courseName = [courses objectAtIndex:indexPath.row]; 

     cell.textLabel.text = courseName; 
     return cell; 

} 

所有你需要做的是落實 - (NSArray的*)coursesInSection:根據需要邏輯(NSInteger的)部分。

+0

對不起,但是你的方法並不工作,因爲我有課程已經聲明爲一個NSDictionary和CourseKeys和一個NSArray。我也從我的PList文件中加載所有項目。 – 2012-07-24 16:57:20

+0

我現在的代碼在這裏:http://pastebin.com/X5uMJqH4 – 2012-07-24 18:07:31

+0

沒關係,只需更改我的代碼有點採用它使用課程作爲NSDictionary。我只是告訴你的方式去,而不是最終的源代碼。) – 2012-07-25 08:19:52