ios - 如何按字母顺序将 pList 分成组,如 iOS 联系人

标签 ios xcode4 ios4

我一直在尝试将已按字母顺序排列的 pList 分离到 TableView 中。例如,当名称从 A 切换到 B 时,我希望花哨的分组表格格式将 A 与 B 分开。

关于如何做到这一点有什么想法吗?

这是我的代码,我在进行排序。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:kEMPLOYEE_PLIST];

NSMutableArray *countries = [[NSMutableArray alloc ]initWithContentsOfFile:path];

// Now the array holds NSDictionaries, sort 'em:
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"FullName" ascending:YES] autorelease];
sortedCountries = [[countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain];

最佳答案

查看 this potentially related question , 但基本上您需要做的是让每个字母(或组)成为一个部分。

首先您需要指定您的表格是分组的(无论是在 Interface Builder 中还是通过 UITableView 的 - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 方法,指定 UITableViewStyleGrouped 作为样式。

然后你要实现:

UITableViewDataSource 的

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

当然还有 UITableViewDataSource 的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。

在最后一个方法中,您可以查看 indexPath.section 以获取要返回的字母表中的字母,并查看 indexPath.row 以返回该字母(组)存在的第 N 个条目。

目前使用这些是否足够?

关于ios - 如何按字母顺序将 pList 分成组,如 iOS 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8276407/

相关文章:

ios - 如何将 Objective-C block 转换为 Swift 闭包?

xcode - 升级到 Mountain Lion 和 XCode 4 打破了我的 "make"?

xcode4 - 关闭 XCode 4 中的其他选项卡吗?

iOS MDM : Can we get to know whether device is rooted or jailbroken?

ios - 如何查找不同iOS设备(非OS版本)上的用户百分比?

ios - 在缓存 iOS Swift 中存储数据数组

xcode4 - XCode : The document: "<document name>" could not be saved. 文件已被另一个应用程序更改”

ios4 - 如何获取 iPad 自定义键盘的图像

iphone - 获取 iPod 轨道的音乐数据

iphone - NSMutableData SetLength错误