10.20-表格视图UITableView
UITableView:
单元格:(UITableViewCell),只有一列
重用机制:凡是没有显示出来的单元格是不需要创建对象的,只有滑动时,才动态的 加载进来。而且向下滑动时,新显示出来的对象用到的单元格就是滑动时上 面消失的单元格。
UITableViewController:的根视图就是一个UITabView
1. 今天根据上课的内容,练习了一下UITableView的使用,UITableView的使用方式如下:
1 #import "ViewController.h" 2 3 @interface ViewController () <UITableViewDataSource, UITableViewDelegate> 4 { 5 NSArray *_array; 6 } 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 //创建一个UITableView对象, 如果样式是UITableViewStylePlain,则分组与分组之间是连续在一起,而且顶部的分组的头部标题(如果有)在滑动时会一直保持在顶部直到下一个分组的头部标题到达顶部才消失;如果样式是UITableViewStyleGrouped,则分组与分组之间有一段空隙,而且顶部的分组头部标题会随滑动为消失。分组与分组之间的 15 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; 16 17 //设置tableView的数据源对象 18 tableView.dataSource = self; 19 20 //设置tableView的代理对象 21 tableView.delegate = self; 22 23 //设置表头视图, 尺寸大小中只有高度有效 24 tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)]; 25 tableView.tableHeaderView.backgroundColor = [UIColor redColor]; 26 27 //设置单元格之间的分隔符颜色 28 tableView.separatorColor = [UIColor yellowColor]; 29 30 //设置表尾视图,尺寸中只有高度有效 31 tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)]; 32 tableView.tableFooterView.backgroundColor = [UIColor greenColor]; 33 34 //将UITableView添加到根视图上 35 [self.view addSubview:tableView]; 36 37 _array = @[@"A", @"B", @"C", @"D"]; 38 } 39 40 #pragma mark - UITableViewDataSource required 41 //返回每一个分组的行数, 42 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 43 { 44 if(section) 45 return 3; 46 return _array.count; 47 } 48 49 //设置每一个分组的每一行的单元格对象 50 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 51 { 52 //该标识符是UITableView的单元格对象的一种重用机制。 53 static NSString *identifier = @"Cell0"; 54 //当一个单元格因为滑动而消失时,此单元格对象会跑出队列,该队列是一个可变字典对象,键既为标识符,键对应的对象是一个可变数组,数组中存储的就是所有标识符为该键所指定的标识符的单元格对象。下面该方法就是通过找到一个跑出队列,并和指定标识符对应的单元格对象来设置一个新的单元格对象,达到重用的目的。就不要为每一个新出现的单元格创建一个单元格对象而造成内存浪费了。 55 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 56 if(cell == nil){ 57 //单元格的样式有四种。 58 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 59 } 60 //设置单元格的文本。 61 cell.textLabel.text = _array[indexPath.row]; 62 return cell; 63 } 64 65 #pragma mark - UITableViewDataSource optional 66 //设置分组个数 67 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 68 { 69 return 4; 70 } 71 72 //设置分组头部的标题 73 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 74 { 75 if(section != 0) 76 return @"多余的分组"; 77 return @"正常的分组"; 78 } 79 80 //设置分组底部的标题 81 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 82 { 83 return @"Footer"; 84 } 85 86 #pragma mark - UITableViewDelegate optional 87 88 //设置分组头部的视图,会覆盖分组头部的标题;坐标完全没有意义,高度取决于分组头部的高度,该高度可以设置。 89 //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 90 //{ 91 // UIView *v = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 50, 10)]; 92 // v.backgroundColor = [UIColor purpleColor]; 93 // return v; 94 //} 95 96 //设置分组头部的高度, 如果tableView是UITableViewStyleGrouped样式,则分组之间的间隙是包括在分组头部中的,因此这里设置的高度会影响间隙的高度。 97 //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 98 //{ 99 // return 10; 100 //} 101 102 //设置选中某个单元格时的行为。 103 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 104 { 105 NSLog(@"Row: %ld, Section: %ld", (long)indexPath.row, (long)indexPath.section); 106 } 107 108 - (void)didReceiveMemoryWarning { 109 [super didReceiveMemoryWarning]; 110 // Dispose of any resources that can be recreated. 111 } 112 @end
2. 对于单元格的重用机制,特别做了一下测试,尝试着自己自定义了一个继承自UITableViewCell的类,来创建自定义的单元格对象
//OtherTableViewCell.h
1 #import <UIKit/UIKit.h> 2 3 @interface OtherTableViewCell : UITableViewCell 4 5 @property (nonatomic, weak) UIImageView *myImageView; 6 7 @end
//OtherTableViewCell.m
#import "OtherTableViewCell.h" @implementation OtherTableViewCell //自定义tableView单元格类,初始化方法。 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){ //单元格的默认高度是44px UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 44, 44)]; _myImageView = imgView; //单元格上的视图应该添加到contentView属性上 [self.contentView addSubview:_myImageView]; } return self; }
在自定义的一个继承自UIViewController的类中创建一个UITableView对象,并在该UITableView对象中使用自定义的cell对象。
//SecondViewController.m
1 #import "SecondViewController.h" 2 #import "OtherTableViewCell.h" 3 4 @interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate> 5 @property (weak, nonatomic) IBOutlet UITableView *_myTableView; 6 7 @end 8 9 @implementation SecondViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 //在tableView中注册一个cell的原型,并指定标识符; 15 [__myTableView registerClass:[OtherTableViewCell class]forCellReuseIdentifier:@"cell4"]; 16 } 17 18 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 19 { 20 return 40; 21 } 22 23 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 24 { 25 //对于storyboard中已经存在的单元格模版,可以不判断下面方法的返回单元格对象是不是空,再重新创建一个,而是可以直接返回。 26 //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath]; 27 28 //通过自定义并注册的cell原型来生成单元格对象。前提是该原型用registerClass: forCellReuseIdentifier:注册过。否则下面指定的标识符没有原型对应的话,返回的cell将是nil。 29 OtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4" forIndexPath:indexPath]; 30 cell.myImageView.image = [UIImage imageNamed:@"NavigationBarLogo.png"]; 31 return cell; 32 } 33 34 @end
通过以上练习,基本知道了UITableView是怎样利用 dequeueReusableCellWithIdentifier : 方法来实现单元格对象的重用了。

浙公网安备 33010602011771号