UITableView
2015-08-08 21:23
288 查看
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor=[UIColor
cyanColor];
self.navigationController.navigationBar.translucent=NO;
self.title=@"表视图";
//按照scrollView的步骤,用自己的初始化方法创建一个TableView
UITableView *tableView=[[UITableView
alloc] initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height-64)
style:UITableViewStylePlain];
[self.view
addSubview:tableView];
[tableView
release];
tableView.backgroundColor=[UIColor
purpleColor];
//设置行高
tableView.rowHeight=100;
//tableView的两套代理方法,
//协议1的代理人
tableView.dataSource=self;
//协议2代理人
tableView.delegate=self;
// Do any additional setup after loading the view.
}
//第一套协议
UITableViewDataSource协议
#pragma mark tableView第一个必须实现的协议方法,指定分区内有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//奇数分区有5行,偶数分区有10行
//先执行设置分区的方法,后执行每个分区有多少行
if (section%2 ==
1) {
return 5;
}else{
return 10;
}
//让数组里的元素个数和行数保持相同
return
self.arr.count;
}
#pragma mark tableView第二个协议方法,主要用来显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//创建相应个数的cell
// static特点:1.只初始化一次 2.如果没有初始值,默认是0
3.直到程序结束才会消失
//当cell显示结束之后,会把cell统一放到重用池中,等需要cell显示了,先从重用池中找,看有没有闲置的cell,如果有的话用闲置的cell,如果没有再创建,
//cell的重用目的是为了节约创建成本,用有限的cell把所有数据都显示出来
//第一句话:给重用池先设置一个重用的标志,根据这个标志可以找到对应的重用池
static NSString *reuse=@"reuse";
//tableView通过重用标志在重用池中寻找cell,如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面则是nil,空
UITableViewCell *cell=[tableView
dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
// cell == nil
//如果没有cell则创建cell
cell=[[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:reuse] autorelease];
}
//对cell进行赋值
//cell里有默认的3个控件:imageView ,2个Label
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString
stringWithFormat:@"%ld",indexPath.section];
cell.imageView.image=[UIImage
imageNamed:@"h7.jpeg"];
NSLog(@"%ld",indexPath.row);
//indexPath.row保存的是行数,从0开始
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//每个分区有一个条,分区的头标题
return
@"动物保护区";
}
#pragma mark 索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *arr=@[@"0",@"1",@"2",@"3",@"4"];
return arr;
}
//第2套:
UITableViewDelegate
#pragma mark tableview的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
NSLog(@"section:%ld,row:%ld",indexPath.section,indexPath.row);
//打印当前点击的人名叫什么
NSLog(@"%@",self.arr[indexPath.row]);
//创建一个下一页
//点击之后跳到下一页
SecondViewController *seVC=[[SecondViewController
alloc]
init];
[self.navigationController
pushViewController:seVC animated:YES];
[seVC
release];
}
//定义一个数组的属性
//重写初始化方法
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self=[super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray
arrayWithObjects:@"宋江",
@"卢俊义",
@"吴用",
@"公孙胜",
@"关胜",
@"林冲",
@"秦明" ,@"呼延灼" ,
@"花容",@"柴进",
@"李应",
@"朱仝",@"鲁智深",@"武松",nil];
}
return
self;
}
[super
viewDidLoad];
self.view.backgroundColor=[UIColor
cyanColor];
self.navigationController.navigationBar.translucent=NO;
self.title=@"表视图";
//按照scrollView的步骤,用自己的初始化方法创建一个TableView
UITableView *tableView=[[UITableView
alloc] initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height-64)
style:UITableViewStylePlain];
[self.view
addSubview:tableView];
[tableView
release];
tableView.backgroundColor=[UIColor
purpleColor];
//设置行高
tableView.rowHeight=100;
//tableView的两套代理方法,
//协议1的代理人
tableView.dataSource=self;
//协议2代理人
tableView.delegate=self;
// Do any additional setup after loading the view.
}
//第一套协议
UITableViewDataSource协议
#pragma mark tableView第一个必须实现的协议方法,指定分区内有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//奇数分区有5行,偶数分区有10行
//先执行设置分区的方法,后执行每个分区有多少行
if (section%2 ==
1) {
return 5;
}else{
return 10;
}
//让数组里的元素个数和行数保持相同
return
self.arr.count;
}
#pragma mark tableView第二个协议方法,主要用来显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//创建相应个数的cell
// static特点:1.只初始化一次 2.如果没有初始值,默认是0
3.直到程序结束才会消失
//当cell显示结束之后,会把cell统一放到重用池中,等需要cell显示了,先从重用池中找,看有没有闲置的cell,如果有的话用闲置的cell,如果没有再创建,
//cell的重用目的是为了节约创建成本,用有限的cell把所有数据都显示出来
//第一句话:给重用池先设置一个重用的标志,根据这个标志可以找到对应的重用池
static NSString *reuse=@"reuse";
//tableView通过重用标志在重用池中寻找cell,如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面则是nil,空
UITableViewCell *cell=[tableView
dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
// cell == nil
//如果没有cell则创建cell
cell=[[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:reuse] autorelease];
}
//对cell进行赋值
//cell里有默认的3个控件:imageView ,2个Label
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString
stringWithFormat:@"%ld",indexPath.section];
cell.imageView.image=[UIImage
imageNamed:@"h7.jpeg"];
NSLog(@"%ld",indexPath.row);
//indexPath.row保存的是行数,从0开始
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//每个分区有一个条,分区的头标题
return
@"动物保护区";
}
#pragma mark 索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *arr=@[@"0",@"1",@"2",@"3",@"4"];
return arr;
}
//第2套:
UITableViewDelegate
#pragma mark tableview的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
NSLog(@"section:%ld,row:%ld",indexPath.section,indexPath.row);
//打印当前点击的人名叫什么
NSLog(@"%@",self.arr[indexPath.row]);
//创建一个下一页
//点击之后跳到下一页
SecondViewController *seVC=[[SecondViewController
alloc]
init];
[self.navigationController
pushViewController:seVC animated:YES];
[seVC
release];
}
//定义一个数组的属性
//重写初始化方法
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self=[super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray
arrayWithObjects:@"宋江",
@"卢俊义",
@"吴用",
@"公孙胜",
@"关胜",
@"林冲",
@"秦明" ,@"呼延灼" ,
@"花容",@"柴进",
@"李应",
@"朱仝",@"鲁智深",@"武松",nil];
}
return
self;
}
相关文章推荐
- 【Android UI】ViewPage
- leetcode - Unique Paths II
- UIStepper
- UIScrollView 按页面滚动视图 UIPageControl
- POJ2478 Farey Sequence(欧拉函数,打表)
- Pixel Width of the text in a UILabel
- UITableView省市区字典
- UI05_Control(UIStepper加减音量/UIBlurEffect毛玻璃/UISlider滑动/UISegmentedControl小模块)
- [leetcode] Implement Queue using Stacks
- UIButton的一些常用知识点
- [leetcode 234]Implement Queue using Stacks
- UVA 10420 List of Conquests
- UIViewController的生命周期
- UIViewController
- opencv开发之MFC的.SetWindowTextA() + OnTimer(UINT_PTR nIDEvent) +cvNamedWindow 在MFC上显示
- UI中简单的猜数字游戏
- UI, 关灯游戏的实现
- Longest Consecutive Sequence
- android自定义UI模板图文详解
- UIScrollView无法滚动的解决办法