UITableView
2015-08-10 09:03
489 查看
TableView的创建及使用方法
// // MainViewController.m // UI08_UITableView // // Created by dllo on 15/8/7. // Copyright (c) 2015年 Clare. 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 magentaColor]; 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]; tableView.backgroundColor = [UIColor colorWithRed:147 / 255.0 green:112 / 255.0 blue:219 / 255.0 alpha:1]; [self.view addSubview:tableView]; [tableView release]; /// 设置行高 tableView.rowHeight = 100; /// tableView的两套代理方法 // 第一套协议************************************************** //// dataSource tableView.dataSource = self; // 第二套协议************************************************** //// delegate设置代理人 tableView.delegate = self; } #pragma mark tableView第一个必须实现的协议方法,指定分区内有多少行 /// tableView必须要有 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 让数组里的元素个数和行数保持相同 // return self.arr.count; // 奇数分区5行,偶数分区有10行 // 先执行设置分区的方法,后执行每个分区有多少行 if (section % 2 == 1) { return 5; } else { return 10; } } #pragma mark 第二个协议方法,主要是用来显示数据 /// table必须要有 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /// 创建cell /// static的特点 // 1.只初始化一次 // 2.如果没有初始值,默认为0 // 3.直到程序结束才会消失 // 当cell显示结束之后,会把cell统一放到重用池中,等需要cell显示了,先从重用池中寻找,如果没有再创建 // cell的重用目的是为了节约创建成本,用有限的cell把所有的数据都显示出来 // 给重用池先设置一个重用的标志,根据这个标志可以找到对应的重用池 static NSString *reuse = @"reuse"; // tableview通过重用标志在重用池中寻找cell,如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面是nil 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:@"05.jpg"]; // NSLog(@"%ld", indexPath.row); // indexPath.row保存的是行数,从0开始 return cell; } #pragma mark tableview里面有多少个section即多少个分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10; } /// tableview常用的点击方法 #pragma mark - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"冰风谷火爆来袭"; } #pragma mark 索引条 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSArray *array = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"]; return array; } // 第二套协议方法********************************************** #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 *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年 Clare. All rights reserved. // #import "SecondViewController.h" #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (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
相关文章推荐
- UI08_TableView界面传值
- PHP 用QueryList抓取网页内容
- UI08_TableView
- POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)
- iOS UI_电话本
- iOS UI_相册
- UI连载五--常用UI控件—Label、Button、UITextField、UIImageView、UISlider、UISwitch、UIActivityIndicatorView等
- iOS UI08_tableView省市区字典数组
- UIViewController
- UITextView
- STL 之 优先队列(priority_queue)
- Regionals 2009 >> Asia - Hsinchu UVALIVE, 4529 A Constrained Queen Game - 搜索剪枝
- UI设计中的结构设计
- POJ 题目2778 DNA Sequence(AC自动机,矩阵快速幂)
- Unique Paths II
- Unique Paths I
- Regionals 2009 Asia - Hsinchu UVALIVE, 4525 - Clues 搜索
- UI019---ITableviewcell的性能问题和缓存优化
- UI018---UITableView使用
- UI017---UITableView的入门