标签:uitableview
(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
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:uitableview
原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47356109