您的位置:首页 > 产品设计 > UI/UE

UITableView

2015-08-11 20:01 483 查看
// 创建tableView

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.backgroundColor = [UIColor whiteColor];
[self.tableView release];


// 设置行高

self.tableView.rowHeight = 70;


// tabelView两套代理方法,dataSource负责数据显示,delegate负责点击效果

// dataSource
self.tableView.dataSource = self;
// delegate
self.tableView.delegate = self;


// tabelView第一个必须实现的协议方法,指定分区内有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 让行数等于数组元素个数
// 奇数分区有5行,偶数分区有10行
// 先执行分区方法,后执行每个分区有都少行
if (section % 2 == 0) {
return 10;
} else {
return 5;
}
}


// 第二个协议方法,主要用来显示数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建相应行数的cell
// static特点 1.只初始化一次 2.如果没有初始值,默认是0 3.直到程序结束才消失
// 当cell显示结束后,会把cell统一放到重用池中,等需要cell显示,先从重用池中寻找闲置cell,有的话用闲置cell,没有就创建
// cell的重用目的是为了节约创建成本,用有限的cell把所有数据显示出来

// 给重用池先设定一个重用标志,根据标志寻找相对应的重用池
// tabelView通过重用标志在重用池中寻找cell.如有显示cell,cell会保存有效地cell对象地址,如果没有,cell里面则是nil
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuse];
// 如果没有cell,则创建
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
// 对cell进行赋值
// cell里默认三个控件
cell.textLabel.text = self.array[indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
cell.imageView.image = [UIImage imageNamed:@"12345.jpg"];

// indexPath.row保存的是行数,从0开始

return cell;
}


// tabelView里有多少个section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
}


// mark 分区的头标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"水浒";
}


// tableView的点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: