IOS UITableView分组与索引分区实例

作者:朱克锋

邮箱: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;

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值