UITableView
2015-08-11 20:01
483 查看
// 创建tableView
// 设置行高
// tabelView两套代理方法,dataSource负责数据显示,delegate负责点击效果
// tabelView第一个必须实现的协议方法,指定分区内有多少行
// 第二个协议方法,主要用来显示数据
// tabelView里有多少个section
// mark 分区的头标题
// 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]; }
相关文章推荐
- UGUI Button
- Request Flow for Provisioning Instance in Openstack
- NSURLRequest 获取下载文件的大小(expectedContentLength)
- 带分区的UITableView的创建(省市区字典)
- [iOS]为什么手动打印UITableViewCell的宽度是320高度是44
- [POJ 1679] The Unique MST 最小树
- UI10_tableView的编辑
- Android学习路径(22)应用Fragment建立动态UI——构建一个灵活UI
- UITouch
- UI10_TableView的编辑
- Android API Guides学习2——Common Intents
- UIView中使用了NSNotificationCenter之后,重入时会发生Crash的现象
- CodeForces-450B Jzzhu and Sequences
- 关于UIScrollView事件
- UITableViewCell cell中图片文字的自适应高度
- UITableViewCell cell中图片文字的自适应高度
- UITableViewCell cell中图片文字的自适应高度
- 116. Distinct Subsequences
- UITableView
- 23.Merge k Sorted Lists (Array, Queue; Sort)