//
// ViewController.m
// 11-29-uitableview-lianxi2
//
// Created by kevin on 13-11-29.
// Copyright (c) 2013年 kevin. All rights reserved.
// 给上一个tableview加上头部标题和尾部标题,并且重构上一个lianxi1的代码
/*
1中间使用了dictionary,怎么定义以及怎么访问,用了两个方法一个是头标题,一个是尾部标题,返回值都是nsstring,很好记。
2中间抽取了字典的title为宏,记得以后用字典都这样写,便于同意修改title。
*/
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
{
NSArray *_people;
#define header @"header"
#define footer @"footer"
#define cities @"cities"
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
// 二逼青年第一次写的时候忘记给tableview设置属性了,找了半天才找到错误
tableView.dataSource = self;
[self.view addSubview:tableView]; // 二笔青年总是忘记这一步————把视图加进去,二逼青年要好好反省。
// 二逼青年试试用用dictionary来初始化带头,尾,以及People-cell内容的数组。
_people = @[
@{
header : @"This is kevin header description",
footer : @"This is kevin footer description",
cities : @[@"跑车",@"豪宅",@"娇妻",@"美妾"]
},
@{
header : @"This is jacky header description",
footer : @"This is jacky footer description",
cities : @[@"拖拉机",@"破房子",@"没老婆"]
}
];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _people.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *getProvince = _people[section];
NSArray *getCity = getProvince[cities]; // 字典的访问也和数组类似用中括号加标示符名字访问。
return getCity.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ // 这个方法就是设置cell的tetlabel的内容,返回的是一个cell
static NSString *ID = @"C1";
// 1.从缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果缓存池中没有可循环利用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
NSDictionary *getProvince = _people[indexPath.section];
NSArray *getCities = getProvince[cities];
cell.textLabel.text = getCities[indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ // 设置头
NSDictionary *getHeader = _people[section];
return getHeader[header];
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{ // 设置尾
NSDictionary *getFooter = _people[section];
return getFooter[footer];
}
@end
UITableView基本使用
最新推荐文章于 2018-10-16 18:27:50 发布