Xcode - TableView PList项目到A-Z部分

时间:2012-07-23 02:50:00

标签: iphone sdk plist tableview sections

所以我有一个list.plist,其中包含我的所有项目。它们都是具有Key和Value的字符串。

我将'课程'声明为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;
}

1 个答案:

答案 0 :(得分: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)部分。

相关问题