作者:朱克锋

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

}

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐