Fork me on GitHub

UI第九讲.UITableView表视图创建,表视图的重用机制,表视图的相关配置方法

一.UITableView表视图创建

   1>.基本属性:
          UITableView继承自UIScrollView,所以可以滚动
          表视图的每一条数据都是显示在UITableViewCell对象中
          表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0始

   2>.重要用法:

          最重要的是两个代理方法 <UITableViewDelegate,UITableViewDataSource>(其中必须实现的是 numberOfRows、cellForRowAtIndexPath)

          section代表分区,row:代表分区内的行,cell代表row对应的内容

DateSource代理方法

delegate的代理方法

 

示例代码:

创建一个UITableView的标准六步法

 

ViewController.m文件的创建步骤

 

 

 

注意:下面是tableView创建的新方法,以后用这个:

 

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
 4 
 5 @property(nonatomic,strong)UITableView *tableView;
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     // Do any additional setup after loading the view, typically from a nib.
14     
15     [self loadTableView];
16     
17 }
18 
19 -(void)loadTableView
20 {
21     self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
22     self.tableView.dataSource = self;
23     self.tableView.delegate = self;
24     [self.view addSubview:self.tableView];
25     
26      //注册tableView
27     [self.tableView  registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
28 }
29 
30 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
31 {
32     return 12;
33 
34 }
35 
36 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
37 {
38     //相当于if判断
39     UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
40 
41    cell.textLabel.text =  @"1233";
42     
43     return cell;
44 }

 

 

 

 

 

二.表视图的重用机制

      UITableViewmutableSet来实现重用功能

      出屏幕的cell会被添加到mutableSet中,进入屏幕的cell,先从set中获取,如果获取不到,才创建一个cell。在cell显示之前,给cell赋上相应的内容。

      cellreuseIdentifier是重用的关键。(用有限的cell来循环使用,这是就是重用)

 

下面的内容就是cell重用机制的体现:(重要的是这个要做判断,先从重用池中取,如果获取不到,就创建一个cell)

 

三.表视图的相关配置方法

 

     1>.设置cell的内容或图片

 

 

        2>.设置cell的高度,分区以及头脚的高度

 

 

       3>.返回头尾视图,以及row的点击触发事件

 

 

   4>indexPath属性

 

 

总结:

 

posted @ 2015-09-18 21:38  DengHuiCheng  阅读(177)  评论(0编辑  收藏  举报