美文网首页
day09-UITableView-07索引条(综合小案例)

day09-UITableView-07索引条(综合小案例)

作者: js_huh | 来源:发表于2019-10-28 09:30 被阅读0次

UITableView-01基本使用
UITableView-02模型优化
UITableView-03复杂的Plist解析
UITableView-04常见属性和样式
UITableView-05可重复使用Cell-有限的创建新的cell
UITableView-06可重复使用cell-注册方式


UITableViewController-01简单使用


效果图

基础设置

  1. 遵循数据源协议,设置数据源代理对象,实现代理方法.
    @interface ViewController ()<UITableViewDataSource>
    self.tableView.dataSource = self;

懒加载

  • 读取Plist文件 pathForResource :
  • 复杂的字典转模型
  • 模型放入数组里面
   -(NSArray*)carArrayData{
   if (_carArrayData == nil) {
       //获取plist文件数据
       NSArray *carAll = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil]];
       //字典转模型
       NSMutableArray * carTemp = [NSMutableArray array];
       for (NSDictionary* dictTemp in carAll) {
           CarGroup * carGroup = [CarGroup carGroupWithDict:dictTemp];
           [carTemp addObject:carGroup];
       }
       self.carArrayData = carTemp;
   }
   return _carArrayData;
   }
//数组里面装的是转换好的Cars模型
@property(nonatomic,strong) NSArray* carsArray;


+(instancetype)carGroupWithDict:(NSDictionary *)dict{
    CarGroup* carGroup = [[self alloc]init];
    carGroup.title = dict[@"title"];
    //1.将plist文件中的cars取出,它是个数组
    //2.将数组的每个元素取出,转换为Cars模型
    //3.将Cars模型放入可变数组,最后用不可变数组封装
    NSMutableArray *carChange = [NSMutableArray array];
    for (NSDictionary* carTemp  in dict[@"cars"]) {
        Cars *car = [Cars carWithDict:carTemp];
        [carChange addObject:car];
    }
    carGroup.carsArray = carChange;
    return carGroup;
}

获取对应数据

  1. 设置总共多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carArrayData.count;
}
  1. 每组分别多少行
//每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CarGroup * groupCar = self.carArrayData[section];
    return groupCar.carsArray.count;
}
  1. 每行显示的内容

    • 1.可重复使用cell -- 注册方式,实现可循环使用的cell
    - (void)viewDidLoad {
    [super viewDidLoad];
    //注册cell.
    [self.tableView registerClass:[CarCell class] forCellReuseIdentifier:ID];
    }
    
    //每行显示的内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    ....
    return cell;
    }
    
    • 2.可重复使用cell -- 创建有限的新cell (initWithStyle)
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
    //创建新的cell,并且绑定ID
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    //        NSLog(@"cell = %@",cell);
    }
    ....
    

dequeueReusableCellWithIdentifier方法讲解


其他的一些方法

  1. 分组展示数据


  2. 组头标题的实现
//每组头部文字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    CarGroup * groupCar = self.carArrayData[section];
    return groupCar.title;
}
  1. 索引条的功能实现
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//    第一种方法:
//    NSMutableArray * titles = [NSMutableArray array];
//    for (CarGroup *temp  in self.carArrayData) {
//        NSString *title = temp.title;
//        [titles addObject:title];
//    }
    
    // 第二种方法: KVO方式,抽取数组中每个对象的title属性的值,放在一个数组中返回
    NSArray<NSString *> *titles = [self.carArrayData valueForKey:@"title"];
    return titles;
}

相关文章

  • day09-UITableView-07索引条(综合小案例)

    UITableView-01基本使用UITableView-02模型优化UITableView-03复杂的Plis...

  • OpenGLES(六)-综合案例:索引绘图

    OpenGLES(六)-综合案例:索引绘图 索引绘图 相信看这篇文章的同学应该对图元装配方式很熟悉了吧?提供一个参...

  • 【Mysql】索引的类型最完整介绍

    一、简介 Mysql主要的几种索引类型: 普通索引 唯一索引 主键索引 综合索引 全文索引 语句 1、unique...

  • java.lang.classnotfoundexception

    day14 tomcat 综合小案例中遇到的java.lang.classnotfoundexception问题 ...

  • 综合案例

    来自拉钩教育-JAVA就业集训营 1.商城案例表设计 通过对商城项目的部分表关系进行分析,完成数据库表的设计 1....

  • 索引条

    【链接】 https://www.jianshu.com/p/5d30f5d16277【效果】

  • 索引条

    http://www.cnblogs.com/justqi/p/7101784.html

  • 索引条

    tableView展示类似通讯录的数据的时候右侧会有索引功能,该功能的实现是非常easy的,苹果系统内部已经为我们...

  • 3 Python基础

    Python基础 1.循环语句 while循环 for循环 1.1 循环语句的基本使用 1.2 综合小案例 1.3...

  • 5.4 索引案例

    在设计索引时,若遇到有对各种组合进行筛选的情况,可以根据情况考虑建立各类组合索引或者建立一个索引,甚至不建立索引....

网友评论

      本文标题:day09-UITableView-07索引条(综合小案例)

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