• AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引


    AJ分享,必须精品

    先看效果图

    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述

    代码 ViewController

    
    #import "NYViewController.h"
    #import "NYCarGroup.h"
    #import "NYCar.h"
    
    @interface NYViewController () <UITableViewDataSource>
    
    @property (nonatomic, strong) UITableView *tableView;
    @property (nonatomic, strong) NSArray *carGroups;
    
    @end
    
    @implementation NYViewController
    
    -(UITableView *)tableView
    {
        if (_tableView == nil) {
            _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
            //设置数据源
            _tableView.dataSource = self;
            //加载上去
            [self.view addSubview:_tableView];
        }
        return _tableView;
    }
    
    //懒加载
    -(NSArray *)carGroups
    {
        if (_carGroups == nil) {
            _carGroups = [NYCarGroup carGroups];
        }
        return _carGroups;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    //    NSLog(@"%@", self.carGroups);
        // 调用tableView添加到视图
        [self tableView];
    }
    
    #pragma mark - tableView 数据源方法
    
    /**分组总数*/
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return self.carGroups.count;
    }
    
    /**每一组多少行 ,section是第几组*/
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        NYCarGroup * group = self.carGroups[section];
        return group.cars.count;
    }
    
    /**单元格*/
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //可重用表示符
        static NSString *ID = @"cell";
    
        //让表格去缓冲区查找可重用cell
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        //如果没有找到可重用cell
        if (cell == nil) {
            //实例化cell
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
    
        //设置cell 内容
        //取出数据模型
        NYCarGroup *group = self.carGroups[indexPath.section];
        NYCar *car = group.cars[indexPath.row];
    
        //设置数据
        cell.imageView.image = [UIImage imageNamed:car.icon];
        cell.textLabel.text = car.name;
    
    
    
        return cell;
    }
    
    
    /**每一组的标题*/
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [self.carGroups[section] title];
    }
    
    /** 右侧索引列表*/
    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        /*
         索引数组中的"内容",跟分组无关
         索引数组中的下标,对应的是分组的下标
            return @[@"哇哈哈", @"hello", @"哇哈哈", @"hello", @"哇哈哈", @"hello", @"哇哈哈", @"hello"];
    
         返回self.carGroup中title的数组
            NSMutableArray *arrayM = [NSMutableArray array];
            for (HMCarGroup *group in self.carGroups) {
                [arrayM addObject:group.title];
            }
            return arrayM;
    
         KVC是cocoa的大招
         用来间接获取或者修改对象属性的方式
         使用KVC在获取数值时,如果指定对象不包含keyPath的"键名",会自动进入对象的内部查找
         如果取值的对象是一个数组,同样返回一个数组
        */
        /*例如:
        NSArray *array = [self.carGroups valueForKeyPath:@"cars.name"];
        NSLog(@"%@", array);
        */
        return [self.carGroups valueForKeyPath:@"title"];
    
    }
    @end
    

    模型代码


    NYCar.h

    //
    //  NYCar.h
    //  06-汽车品牌带右侧索引
    
    
    #import <Foundation/Foundation.h>
    
    @interface NYCar : NSObject
    
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, copy) NSString *icon;
    
    -(instancetype) initWithDict:(NSDictionary *)dict;
    +(instancetype) carWithDict:(NSDictionary *)dict;
    // 传入一个包含字典的数组,返回一个HMCar模型的数组
    +(NSArray *) carsWithArray:(NSArray *)array;
    @end
    

    NYCar.m

    //
    //  NYCar.m
    //  06-汽车品牌带右侧索引
    //
    //  Created by apple on 15-3-29.
    //  Copyright (c) 2015年 znycat. All rights reserved.
    //
    
    #import "NYCar.h"
    
    @implementation NYCar
    
    -(instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            [self setValuesForKeysWithDictionary:dict];
        }
        return self;
    }
    
    +(instancetype)carWithDict:(NSDictionary *)dict
    {
        return [[self alloc] initWithDict:dict];
    }
    
    +(NSArray *)carsWithArray:(NSArray *)array
    {
    
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [arrayM addObject:[self carWithDict:dict]];
        }
        return arrayM;
    }
    
    - (NSString *)description
    {
        return [NSString stringWithFormat:@"<%@: %p> {name: %@, icon: %@}",self.class, self, self.name, self.icon ];
    }
    @end
    

    NYCarGroup.h

    //
    //  NYCarGroup.h
    //  06-汽车品牌带右侧索引
    
    #import <Foundation/Foundation.h>
    
    @interface NYCarGroup : NSObject
    /** 首字母 */
    @property (nonatomic, copy) NSString *title;
    /** 车的数组,存放的是HMCar的模型数据 */
    @property (nonatomic, strong) NSArray *cars;
    
    -(instancetype) initWithDict:(NSDictionary *)dict;
    +(instancetype) carGroupWithDict:(NSDictionary *)dict;
    +(NSArray *) carGroups;
    @end
    

    NYCarGroup.m

    //
    //  NYCarGroup.m
    //  06-汽车品牌带右侧索引
    //
    
    
    #import "NYCarGroup.h"
    #import "NYCar.h"
    @implementation NYCarGroup
    -(instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            //        [self setValuesForKeysWithDictionary:dict];        
            // dict[@"cars"]存放的是字典的数组
            // 希望将字典的数组转换成HMCar模型的数组
            //        [self setValue:dict[@"cars"] forKey:@"cars"];
            [self setValue:dict[@"title"] forKeyPath:@"title"];
            self.cars = [NYCar carsWithArray:dict[@"cars"]];
    
        }
        return self;
    }
    
    +(instancetype)carGroupWithDict:(NSDictionary *)dict
    {
        return [[self alloc] initWithDict:dict];
    }
    
    +(NSArray *)carGroups
    {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil]];
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [arrayM addObject:[self carGroupWithDict:dict]];
        }
        return arrayM;
    }
    
    - (NSString *)description
    {
        return [NSString stringWithFormat:@"<%@: %p> {title: %@, cars: %@}", self.class, self, self.title, self.cars];
    }
    
    @end
    

    代码偶了

    注意点

    实现右侧索引:

    /* 右侧索引列表/
    -(NSArray )sectionIndexTitlesForTableView:(UITableView )tableView


    对dataSource复习

    @required的两个
    /**单元格*/
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    /**每一组多少行 ,section是第几组*/
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    /*分组总数/
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    /*每一组的标题/
    -(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section


    出错误了,求助失败[apple mach - o linker error]

    听说学iOS百度没用。。。今天真信了,出了个错误,
    【apple mach - o linker error】
    如图

    喵呜


    {
    常见错误描述:
    Apple Mach-O Linker Error这类错误的错误信息最后一行通常如下:
    Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang failed with exit code 1
    发生这种错误的原因通常是因为项目中存在同名类造成链接错误。

    有可能是你存在两个类名称都一样,也可能是因为你在不同的.m文件中定义了同样的const变量。

    这类错误需要自己看错误信息中给出的大长串路径,从中找出你的那个重名类或者变量名称,以此来定位错误位置。
    }

    好了 我只想说,AJ自己把代码拷贝出来,然后又关了xcode ,最后重新建立了一个项目,把代码不动得放回,就这么华丽丽的好了。。。。。。。。。。。。
    
  • 相关阅读:

    CreateProcess
    luogu P2234 [HNOI2002]营业额统计 |平衡树
    luogu P2286 [HNOI2004]宠物收养场 |平衡树
    luogu P3369 【模板】普通平衡树
    luogu P3834 【模板】可持久化线段树 1(主席树)| 静态第k小问题
    luogu P4149 [IOI2011]Race |点分治
    luogu P2634 [国家集训队]聪聪可可 |点分治
    luogu P4178 Tree |点分治+树状数组
    luogu P2664 树上游戏 |点分治
  • 原文地址:https://www.cnblogs.com/luolianxi/p/4990374.html
Copyright © 2020-2023  润新知