史上最简洁的UITableView Sections 展示包含NSDicionary 的NSArray
2013-11-14 17:43
357 查看
这个最典型的就是电话本,然后根据A-Z分组, 当然很多例子,不过现在发现一个很简洁易懂的:
1. 准备数据,定义一个dictionary来显示所有的内容,这个dictionary对应的value全是数组
也就是:
A -> A1, A2...
B -> B1, B2...
...
2. 创建所有的Keys
3. 把Key对应的空数组填满,这个很简单应该能看懂
4. 下面就排序
5. 有了上面的方法之后,就执行UITableView必须的方法
差不多了,这个是我见过最简洁的例子了。
参考:http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
1. 准备数据,定义一个dictionary来显示所有的内容,这个dictionary对应的value全是数组
也就是:
A -> A1, A2...
B -> B1, B2...
...
NSMutableDictionary *sections;
2. 创建所有的Keys
BOOL found; // Loop through the books and create our keys for (NSDictionary *book in self.books) //self.books 就是包含NSDictionary的数组 { NSString *c = [[book objectForKey:@"title"] substringToIndex:1]; found = NO; for (NSString *str in [self.sections allKeys]) { if ([str isEqualToString:c]) { found = YES; } } if (!found) { [self.sections setValue:[[NSMutableArray alloc] init] forKey:c]; } }
3. 把Key对应的空数组填满,这个很简单应该能看懂
for (NSDictionary *book in self.books) { [[self.sections objectForKey:[[book objectForKey:@"title"] substringToIndex:1]] addObject:book]; }
4. 下面就排序
for (NSString *key in [self.sections allKeys]) { [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]]; }
5. 有了上面的方法之后,就执行UITableView必须的方法
#pragma mark - #pragma mark Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.sections allKeys] count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSDictionary *book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; cell.textLabel.text = [book objectForKey:@"title"]; cell.detailTextLabel.text = [book objectForKey:@"description"]; return cell; }
差不多了,这个是我见过最简洁的例子了。
参考:http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
相关文章推荐
- UI 一一 UITableView多组和单组数据展示案例
- UITableView 数据展示 cell
- 菜鸟成长记-QQ列表展示、UITableView强化
- Core Data浅谈系列之五 : 在UITableView中展示
- iOS Quick Tip – programmatically hiding sections of a UITableView with static cells
- 在UIViewController里用UITableView及相关的tableHeaderView、tableFooterView实现三级连动页面页面展示效果
- 修改UITableViewStyleGrouped形式的tableview的背景色 此博文包含图片 (2013-09-10 11:11:57)转载▼ 分类: IOS笔记 在使用UITable
- iphone开发之表格组件UITableView的使用(三)通过加载plist文件字典转模型方式展示分组数据
- IOS UITableView详解一数据展示 & 页面简单实现
- UITableView-多组数组展示
- UITableView实现多组汽车展示(模型里面嵌套模型)
- 关于点击UIScrollView跳转的问题,以及怎么在UITableView上展示滚动视图,九宫格,自定义Cell等
- ViewPager实现页面切换 (包含App第一次登录时候的欢迎界面的展示)
- 用JSONKit变换的NSArray用UITableView的数据源总报错的解决方案
- IOS开发UI展示之UITableView ──分页加载更多
- iOS uitableview自定义相册(实现拍照并保存在指定目录以相册的形式展示图片)
- IOS UITableView 实现LOL数据展示
- UITableView_4cell简单练习,汽车品牌展示
- IOS开发UI展示之UITableView ──分页加载更多
- ios开发——用UITableView展示多行多列图片