IOS 之tableview 分组,索引

参考文档:http://blog.csdn.net/dongstone/article/details/7428157

这里面需要的是使用了静态文件Property List.plist

需要对其进行初始化。

.h

#import <UIKit/UIKit.h>

@interface YYWViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) NSDictionary *listData;
@property(nonatomic,strong) NSArray *keys;
@end

.m

//
//  YYWViewController.m
//  Sections
//
//  Created by yangyw on 13-5-3.
//  Copyright (c) 2013年 yangyw. All rights reserved.
//

#import "YYWViewController.h"

@interface YYWViewController ()

@end

@implementation YYWViewController




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //获取数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    _listData=dict;
    //获取Key,并正序排序
    NSArray *arr=[[_listData allKeys]sortedArrayUsingSelector:@selector(compare:)];
    _keys=arr;
    [self addTableView];
    
}

//添加tableview
-(void) addTableView
{
    UITableView *tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStyleGrouped];
    [tableview setDelegate:self];
    [tableview setDataSource:self];
    
    
    [self.view addSubview: tableview];
    [tableview release];
  }

//很多关于tableview的方法全是托管,就跟.net中的很多事件一样
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //返回行数
    //return [self.listData count];
    
    //获取当前分区所对应的键(key)。在这里键就是分区的标示。
    NSString *key=[self.keys objectAtIndex:section];
    //获取键所对应的值(数组)。
    NSArray *nameSec=[self.listData objectForKey:key];
    //返回所在分区所占多少行。
    return  [nameSec count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //获得所在分区的行数
    NSInteger row=[indexPath row];
    //获得分区值
    NSInteger section=[indexPath section];
    //利用分区获得键值
    NSString *key=[self.keys objectAtIndex:section];
    //利用键值获得其所对应的值
    NSArray *MySectionArr=[self.listData objectForKey:key];
    //定义标记,用于标记单元格
    static NSString *SectionTableMyTag=@"dong";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SectionTableMyTag];
    //如果当前cell没被实例(程序一开始就会运行下面的循环,直到屏幕上所显示的单元格格全被实例化了为止,没有显示在屏幕上的单元格将会根据定义好的标记去寻找可以重用的空间来存放自己的值)
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionTableMyTag];
    }
    cell.textLabel.text=[MySectionArr objectAtIndex:row];
    return  cell;
}

//选中之前进行处理
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   // NSUInteger row = [indexPath row];
    //这样就限制了第一行没法进行选择
    //if (row == 0) {
    //return nil;
    // }
    return indexPath;
}
//选中之后进行处理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //获得行号
    NSInteger row=[indexPath row];
    //获得分区值
    NSInteger section=[indexPath section];
    //利用分区获得键值
    NSString *key=[self.keys objectAtIndex:section];
    //利用键值获得其所对应的值
    NSArray *MySectionArr=[self.listData objectForKey:key];
    NSString *rowValue=[MySectionArr objectAtIndex:row];
    NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected!"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"Yes I Did"
                                          otherButtonTitles:nil];
    [alert show];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //返回(向系统发送)分区个数,在这里有多少键就会有多少分区。
    return  [self.keys count];
}

//把每个分区打上标记key
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *key=[self.keys objectAtIndex:section];
    return key;
}

//在单元格最右放添加索引
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keys;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [super dealloc];
}
@end

 

posted @ 2013-05-03 16:42  Peter_youny  阅读(1442)  评论(0编辑  收藏  举报