(http://img.blog.csdn.net/20150808103801391)
//
// MainViewController.m
// UI08_UITableView
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@end
@implementation MainViewController
-(void)dealloc
{
[_arr release];
[super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor yellowColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationItem.title=@"表视图";
UITableView *tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
tableView.backgroundColor=[UIColor cyanColor];
[self.view addSubview:tableView];
[tableView release];
//设置行高
tableView.rowHeight=100;
//两套代理的方法
tableView.dataSource=self;
//第二套协议代理人
tableView.delegate=self;
}
#pragma mark tableView第一个必须实现的协议方法,指定分区内有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//让数组里的元素个数和行数保持同样
// return self.arr.count;
//奇数分区有五行,偶数分区有十行
//先运行设置分区的方法,后运行每一个分区有多少行
if (section % 2 == 1) {
return 5;
}else{
return 10;
}
}
#pragma mark 第二个协议方法,主要用来显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建相应个数的cell
//static 特点 1.仅仅初始化一次2.假设没有初始值,默认是0 3.直到程序结束,才会消失
//当cell显示结束之后,会把cell统一放到重用池中,等须要cell显示了,先从重用池中找,看有没有闲置cell,假设有就用闲置的cell,假设没有在创建
//cell的重用目的是为了节约成本,用有限的cell把全部数据都显示出来
//给重用池设置一个重用的标志,依据这个标志找到相应的重用池
//tableview通过重用标志在重用池中寻找cell,假设有闲置的cell,cell会保存一个有效地cell对象地址,假设没有,cell里则为nil,空
static NSString *reuse=@"reuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
//假设没有cell,则进行cell的创建
if (!cell) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
//对cell进行赋值
//cell中有三个默认控件
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@"%ld",indexPath.section];
cell.imageView.image=[UIImage imageNamed:@"scratch.png"];
//indexPath保存的行数,从0開始,
NSLog(@"%ld",indexPath.row);
return cell;
}
#pragma mark tableview里有多少个section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
//分区的头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"水浒";
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.arr;
}
//第二套协议
#pragma mark table的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section:%ld,row:%ld",indexPath.section,indexPath.row);
//打印点击的人名是什么
NSLog(@"%@",self.arr[indexPath.row]);
//点击之后跳到下一页
SecondViewController *secVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// SecondViewController.m
// UI08_UITableView
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor orangeColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end