作者:朱克锋

邮箱: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
13
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:3 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐