码迷,mamicode.com
首页 > 其他好文 > 详细

开发进阶13_UITableView多行显示

时间:2014-10-28 00:23:19      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   使用   for   

UITableView的两种内置样式


UITableViewStylePlain                                                       UITableViewStyleGrouped        
bubuko.com,布布扣              bubuko.com,布布扣
 
协议UITableViewDataSource必须实现两个方法cellForRowAtlndexPath和numberOfRowsInSection
 

 

/*

 展示两组数据

 广东:广州、深圳、梅州、东莞

 湖南:长沙、益阳、岳阳

 湖北:黄冈、武汉

 */

 

@interface ViewController () <UITableViewDataSource>

{

    NSArray *_allCities;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1.添加UITableView

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    tableView.dataSource = self;

    

    [self.view addSubview:tableView];

    _allCities = @[

                   @[@"广州",@"深圳",@"梅州",@"东莞"],

                   @[@"长沙",@"益阳",@"岳阳"],

                   @[@"黄冈",@"武汉"]

                   ];

 

}

 

#pragma mark - 数据源方法

 

#pragma mark 一共有多少组(section == 区域\组)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return _allCities.count;

}

 

#pragma mark 第section组一共有多少行

//传入的section的NSInteger类型,从0开始,0表示第一组

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSArray *sectionArray = _allCities[section];

    return sectionArray.count;

}

 

#pragma mark 返回每一行显示的内容(每一行显示怎样的cell)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //indexPath标识唯一的一行

    //第section组的第row行

    //indexPath.row

    //indexPath.section

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    

    //设置cell显示的文字

    cell.textLabel.text = _allCities[indexPath.section][indexPath.row];

    return cell;

}

 

#pragma mark 第section组显示的头部标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return @"hahah";

}

 

#pragma mark 第section组显示的尾部标题

- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    return @"ooo";

}

@end

 
 
使用字典进行重构

#import "ViewController.h"

#define  kHeader @"header"

#define  kFooter @"footer"

#define  kCities @"cities"

 

@interface ViewController () <UITableViewDataSource>

{

    NSArray *_allProvinces;//所有省份

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //1.添加UITableView

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    tableView.dataSource = self;

    [self.view addSubview:tableView];

    _allProvinces = @[

                   @{

                       kHeader:@"广东",

                       kFooter:@"广东好啊",

                       kCities:@[@"广州",@"深圳",@"梅州",@"东莞"]

                    },

                   @{

                       kHeader:@"湖北",

                       kFooter:@"广东也好啊",

                       kCities:@[@"长沙",@"益阳",@"岳阳"]

                    }

                ];

}

 

#pragma mark - 数据源方法

 

#pragma mark 一共有多少组(section == 区域\组)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return _allProvinces.count;

}

 

#pragma mark 第section组一共有多少行

//传入的section的NSInteger类型,从0开始,0表示第一组

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSArray *sectionArray = _allProvinces[section][kCities];

    return sectionArray.count;

}

 

#pragma mark 返回每一行显示的内容(每一行显示怎样的cell)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //indexPath标识唯一的一行

    //第section组的第row行

    //indexPath.row

    //indexPath.section

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    

    //设置cell显示的文字

    cell.textLabel.text = _allProvinces[indexPath.section][kCities][indexPath.row];

    return cell;

}

 

#pragma mark 第section组显示的头部标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return _allProvinces[section][kHeader];

}

 

#pragma mark 第section组显示的尾部标题

- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    return _allProvinces[section][kFooter];

}

@end

 
 
还可以使用模型进行代码重构,这样将会更加方便(跟C#中的一样),同事提供一个类方法对数据进行初始化
 
UITableView总结
1、数据展示的条件
-》UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象
-》要相当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法
-》当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源方法),UITableView会根据方法返回值决定展示怎样的数据
 
2、数据展示过程
-》先调用数据源的

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

         得知一共有多少组

 

    -》然后调用数据源的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

     得知第section组一共有多少行

 

    -》然后调用数据源的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    //indexPath标识唯一的一行

    //第section组的第row行

    //indexPath.row

    //indexPath.section

 

    -》第section组显示怎样的头部标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

 

    -》第section组显示怎样的尾部标题

- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

 

 
 
显示索引条
 

#pragma mark 返回表格右边的索引条

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    //返回的内容没有要求,点击的时候根据点击的数据在数组中的位置,显示相应位置上的UITableViewCell

    return @[@"1",@"2",@"3",@"4",@"5"];

}

 
拼音处理库
Pinyin4Objc

开发进阶13_UITableView多行显示

标签:style   blog   http   io   color   os   ar   使用   for   

原文地址:http://www.cnblogs.com/yaofch107/p/4055446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!