我一直在尝试将已按字母顺序排列的 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 的
在最后一个方法中,您可以查看 indexPath.section 以获取要返回的字母表中的字母,并查看 indexPath.row 以返回该字母(组)存在的第 N 个条目。
目前使用这些是否足够?
关于ios - 如何按字母顺序将 pList 分成组,如 iOS 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8276407/