zoukankan      html  css  js  c++  java
  • 猫猫学IOS(十三)UI之UITableView学习(下)汽车名牌带右侧索引

    猫猫分享,必须精品

    素材代码地址:http://blog.csdn.net/u013357243/article/details/44727225
    原文地址:http://blog.csdn.net/u013357243?viewmode=contents

    先看效果图

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

    代码 ViewController

    //ps:新建iOS交流学习群:304570962 
    可以加猫猫QQ:1764541256 或则微信znycat 
    让我们一起努力学习吧。 
    原文:http://blog.csdn.net/u013357243?viewmode=contents
    //  NYViewController.m
    //  06-汽车品牌带右侧索引
    //
    //  Created by apple on 15-3-29.
    //  Copyright (c) 2015年 znycat. All rights reserved.
    //
    
    #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-汽车品牌带右侧索引
    //
    //  Created by apple on 15-3-29.
    //  Copyright (c) 2015年 znycat. All rights reserved.
    //
    
    #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-汽车品牌带右侧索引
    //
    //  Created by apple on 15-3-29.
    //  Copyright (c) 2015年 znycat. All rights reserved.
    //
    
    #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-汽车品牌带右侧索引
    //
    //  Created by apple on 15-3-29.
    //  Copyright (c) 2015年 znycat. All rights reserved.
    //
    
    #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】
    如图

    喵呜

    度娘n久,问了8个iOS相关的学习群,得到的无外乎这样
    {
    常见错误描述:
    Apple Mach-O Linker Error这类错误的错误信息最后一行通常如下:
    Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang failed with exit code 1
    发生这种错误的原因通常是因为项目中存在同名类造成链接错误。

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

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

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

    (偶喵了个咪闹)
    iOS这部分需要我们大家一起努力啊!
    !!!分享我们的成功,喵呜

    ps:新建iOS交流学习群:304570962
    可以加猫猫QQ:1764541256 或则微信znycat
    让我们一起努力学习吧。
    原文:http://blog.csdn.net/u013357243?viewmode=contents

  • 相关阅读:
    吴裕雄 python 机器学习——集成学习AdaBoost算法回归模型
    吴裕雄 python 机器学习——集成学习AdaBoost算法分类模型
    吴裕雄 python 机器学习——半监督学习LabelSpreading模型
    吴裕雄 python 机器学习——半监督学习标准迭代式标记传播算法LabelPropagation模型
    吴裕雄 python 机器学习——人工神经网络感知机学习算法的应用
    【2017"百度之星"程序设计大赛
    【Uva 1289】Stacking Plates
    【Uva 12093】Protecting Zonk
    【Uva 1633】Dyslexic Gollum
    【2017 Multi-University Training Contest
  • 原文地址:https://www.cnblogs.com/znycat/p/4419182.html
Copyright © 2011-2022 走看看