Xcode-TableView将项目列表到A-Z部分中



所以我有一个list.plist,里面有我所有的项。它们都是带Key和Value的字符串。

我已将"courses"声明为NSDictionary,将"couseKeys"声明为NSArray

为了将所有项目加载到我的表视图中,我使用了以下代码

我的问题是,如何使每个起始字母都有自己的部分,这样我就可以轻松地在字母之间导航?

我知道我可以设置IndexTitlesForTableView部分,我可以在右侧边栏上显示字母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;
}

您应该实现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*)课程章节:(NSInteger)章节。

最新更新