【问题标题】:Xcode - TableView PList Items into Sections A-ZXcode - TableView PList 项目到 A-Z 部分
【发布时间】:2012-07-23 02:49:59
【问题描述】:

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

我将“课程”声明为NSDictionary,将“课程密钥”声明为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;
}

【问题讨论】:

  • 我喜欢 searchArray 的初始化方式... :(
  • 不确定你的意思,我对 Objective-c 很陌生,坦率地说,我知道的不多。

标签: iphone sdk plist tableview sections


【解决方案1】:

您应该实现 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)section。

【讨论】:

  • 抱歉,您的方法不起作用,因为我已经将课程声明为 NSDictionary 和 CourseKeys 以及 NSArray。我还从 PList 文件加载我的所有项目。
  • 没关系,只需稍微更改我的代码以使用课程作为 NSDictionary 来采用它。我只是向您展示了要走的路,而不是最终的源代码。)
  • 对不起,我对 Obj-C 编码很陌生,我不知道该怎么做。我被困住了,任何更多的提示都会有所帮助。我不知道如何处理课程内节
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 2020-05-06
相关资源
最近更新 更多