美文网首页
Cell单元格的复用

Cell单元格的复用

作者: dsosp | 来源:发表于2016-04-21 00:54 被阅读189次

1.单元格复用的实现方式:

1.判断没有可复用的单元格后,由我们自己负责创建 cell 实例。

2.如果没有取到可复用的单元格,由系统自动依据我们提前说明(注册)的类型,帮我们创建出 cell的实例。

  • step1.在 viewDidLoad 中,使用 tableView 的 registerClass 方法,提前注册一个类型,在取不到 cell 的时候,系统会根据我们注册的类型来自动创建 cell。
- (void)viewDidLoad{
//    注册单元格类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
  • step2.在回答第三问时,使用带有两个参数的 dequeReusableCell 即可
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//调用 dequeReusableCell 从队列中取出 cell,如果没有取到,系统会根据我们注册的类型,自动创建可复用的 cell 实例
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    配置 cell
     cell.textLabel.text = @"Hello World";
     return cell;
}

2.自定义内容视图

  • 1.创建显示内容的视图
  • 2.将这些视图以子视图的形式,添加到 cell.contentView 中即可。
//    **2.自定义内容视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //    配置 cell
    
    UILabel *label = [cell.contentView viewWithTag:100];
    if (label == nil) {//如果 cell 中 没有我们自己定义的这个 label 那么我就创建一个添加到 cell 中
        
        label = [[UILabel alloc]init];
        label.tag = 100;
        label.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);//尽量用 bounds 代替 frame 来设置尺寸
        label.font = [UIFont italicSystemFontOfSize:18];
        
//        添加背景可以覆盖掉之前的内容但是不能这么用。
//        label.backgroundColor = [UIColor lightGrayColor];
        
        label.textColor = [UIColor greenColor];
        label.textAlignment = NSTextAlignmentCenter;
    
        [cell.contentView addSubview:label];
    }
    label.text = [NSString stringWithFormat:@"这是第%ld行",indexPath.row];
    return cell;
}

3.自定义辅助视图

s1.创建视图实例。

s2.将视图实例赋值给 cell.accessoryView 即可。

//    *************** 自定义辅助视图 *************
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //    配置 cell    
    if (indexPath.row == 2) {
        UISwitch *mySwitch = [[UISwitch alloc]init];
        mySwitch.on = YES;
        
        [mySwitch addTarget:self action:@selector(clickSwitch:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView  = mySwitch;
    }else{
//        防止复用时产生多个 Switch 开关
        cell.accessoryView = nil;
    }
    return cell;
}
-(void)clickSwitch:(UISwitch*)sender{
    NSLog(@"当前是%@",sender.on?@"open":@"closs");
}

4.表格结合各种数据模型的显示

前提:表格的行数不定,也叫动态表格(例如新闻列表)


1.
#import "MyTableViewController.h"

@interface MyTableViewController ()
//定义两个数组,分别存储 城市和人口信息
@property(nonatomic,strong) NSArray *allCitys;
@property(nonatomic,strong) NSArray *allPopulation;

@end

@implementation MyTableViewController
//推荐懒加载 延迟加载--在第一次使用 的时候,才创建实例
-(NSArray *)allCitys{
    if (_allCitys == nil) {//!_allCitys or nil == _allCitys 皆可以
        _allCitys = @[@"北京",@"上海",@"广州",@"深圳",@"南京"];
    }
    return _allCitys;
}
-(NSArray *)allPopulation{
/*
    NSNumber *num = @100;//也是一种封装方式
*/
    if (!_allPopulation) {
        _allPopulation = @[@3000,@2000,@2500,@2048,@1500];//??
    }
    return _allPopulation;
}

//
//    if (!_allPopulation) {
//        _allPopulation = @[@"3000",@"2000",@"2500",@"2048",@"1500"];
//    }
//    return _allPopulation;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" /*forIndexPath:indexPath*/];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
//    第几行显示第几个元素
//    
    cell.textLabel.text = self.allCitys[indexPath.row];
//    
//    cell.detailTextLabel.text = self.allPopulation[indexPath.row];
//
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@万",self.allPopulation[indexPath.row]];
//    
    cell.imageView.image = [UIImage imageNamed:@"head"];

    return cell;
}

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    获取点中的 cell 实例
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//???
    
    NSLog(@"%@的人口是%@",cell.textLabel.text,cell.detailTextLabel.text);
    
}

@end

相关文章

  • Cell单元格的复用

    1.单元格复用的实现方式: 1.判断没有可复用的单元格后,由我们自己负责创建 cell 实例。 2.如果没有取到可...

  • 点击Cell,实现右边出现对号的功能

    bug,双击单元格的时候,会出现单元格cell的对号,一致不消失. cell.h层 cell.m 层 // 对号显...

  • TableViewCell的复用

    坑:滑动tableView,cell会添加到复用的cell上 初步判断,是cell的复用有问题,看了看,有这么个方...

  • iOS 开发随记

    1、TableViewCell 复用 复用 复用的原则就是样式相同,包括cell的accessoryView或a...

  • RAC用于UITableViewCell复用时的问题

    UITableViewCell和UICollectionViewCell复用使用RAC的问题,解决复用cell中信...

  • 关于cell复用的问题

    cell复用是一个很常见的问题,就如下设置是会造成复用的: UITableViewCell *cell = [ta...

  • Cell的复用

    工作一年多的时间,本以为对Cell的复用已经理解透彻了,然而通过这次版本的迭代开发,发现自己远没有达到透彻的标准。...

  • ReactiveCocoa在UITableViewCell复用的

    1.cell的复用 之前遇到一个蛋疼问题,就是cell中又按钮,当订阅按钮点击事件时候,结果因为cell的复用,出...

  • OC TabelView笔记

    Cell复用 [tableView dequeueReusableCellWithIdentifier:@"cel...

  • 4.2 UiPathExcel之写入操作

    Excel 写操作写一个单元格控件介绍Write Cell: 使用Write Cell控件,在指定单元格写入内容 ...

网友评论

      本文标题:Cell单元格的复用

      本文链接:https://www.haomeiwen.com/subject/agktrttx.html