IOS UITableView分组与索引分区实例
2013-04-11 11:32
387 查看
邮箱:zhukefeng@iboxpay.com
转载请注明出处:http://blog.csdn.net/linux_zkf
示例效果如图
分组图示 索引图示
#import <UIKit/UIKit.h>
@interface SectionsViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
NSDictionary *names;
NSArray *keys;
}
@property (nonatomic, retain) NSDictionary *names;
@property (nonatomic, retain) NSArray *keys;
@end
#import "SectionsViewController.h"
@implementation SectionsViewController
@synthesize names;
@synthesize keys;
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]
initWithContentsOfFile:path];
self.names = dict;
[dict release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:
@selector(compare:)];
self.keys = array;
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [keys count];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
NSString *key = [keys objectAtIndex:section];
return key;
}
索引分区只是要添加这一个代理方法即可实现
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}
转载请注明出处:http://blog.csdn.net/linux_zkf
示例效果如图
分组图示 索引图示
#import <UIKit/UIKit.h>
@interface SectionsViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
NSDictionary *names;
NSArray *keys;
}
@property (nonatomic, retain) NSDictionary *names;
@property (nonatomic, retain) NSArray *keys;
@end
#import "SectionsViewController.h"
@implementation SectionsViewController
@synthesize names;
@synthesize keys;
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]
initWithContentsOfFile:path];
self.names = dict;
[dict release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:
@selector(compare:)];
self.keys = array;
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [keys count];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
NSString *key = [keys objectAtIndex:section];
return key;
}
索引分区只是要添加这一个代理方法即可实现
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}
相关文章推荐
- IOS UITableView分组与索引分区实例
- IOS UITableView分组与索引分区实例
- iOS之UITableView的使用——多分区表格以及分区索引
- (3) iphone 开发 从应用程序看UITableView的:分组,分区,索引,工作原理及其变换法则,plist文件数据定义规则
- IOS开发(52)之UITableView索引搜索之UILocalizedIndexedCollation
- IOS学习笔记-UITableView表索引的颜色设置
- IOS第七天(3:UiTableView 模型和数据的分组的显示)
- IOS学习之UITableView(三):进阶篇索引,标记和自定义的table
- iOS去掉UITableView分组时上方空白区域的方法
- ios开发-UI-UITableView及一个小实例
- iOS 之 UITableView 使用索引
- iOS:分组的表格视图UITableView,可以折叠和展开
- ios学习笔记之-UITableView的分组显示
- IOS UITableView颜色设置的实例详解
- IOS学习笔记-UITableView绑定数据实例
- IOS UITableView索引排序功能
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
- iOS学习之UITableView(三):进阶篇索引,标记和自定义的table
- iOS开发 自定义tableView样式(使用代码/使用Interface Builder)、分组显示、给TableView增加索引、给TableView增加SearchBar
- ios-day06-01(UITableView的使用:分组、设置数据源、UITableViewDataSource协议等)