您的位置:首页 > 产品设计 > UI/UE

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: