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 ,最后重新建立了一个项目,把代码不动得放回,就这么华丽丽的好了。。。。。。。。。。。。
AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引的更多相关文章
- AJ学IOS 之二维码学习,快速打开相机读取二维码
AJ分享,必须精品 上一篇文章写了怎么生成二维码,这儿就说说怎么读取吧,反正也很简单,iOS封装的太强大了 步骤呢就是这样: 读取二维码需要导入AVFoundation框架#import <AV ...
- AJ学IOS 之二维码学习,快速生成二维码
AJ分享,必须精品 二维码是一项项目中可能会用到的,iOS打开相机索取二维码的速度可不是Android能比的...(Android扫描二维码要来回来回晃...) 简单不多说,如何把一段资料(网址呀,字 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- AJ学IOS(28)UI之Quartz2D简单介绍
AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- AJ学IOS(56)网络基础以及如何搭建服务器
AJ分享,必须精品 一:为什么要学习网络编程 关于这个问题,为什么要学习网络编程,AJ的理解就是,这东西是时代发展的必要,没什么为什么,就是应该学,除非你就是想玩单机,但是就算是单机也会有购买金币之类 ...
- IOS开发UI基础UITableView的属性
UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSour ...
随机推荐
- 零售CRM系统开发的核心功能
在零售行业中,客户关系管理系统是一个包含销售,市场营销和客户服务流程的中央枢纽.它为企业所有者提供了一种可以结合所有与销售有关的问题并管理销售流程的有效工具.零售CRM可以留住客户,提供个性化的一流客 ...
- Java基础语法(3)-运算符
title: Java基础语法(3)-运算符 blog: CSDN data: Java学习路线及视频 1.算术运算符 算术运算符的注意问题 如果对负数取模,可以把模数负号忽略不记,如:5%-2=1. ...
- 痞子衡嵌入式:记录i.MXRT1060驱动LCD屏显示横向渐变色有亮点问题解决全过程(提问篇)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是i.MXRT1060上LCD横向渐变色显示出亮点问题的分析解决经验. 痞子衡前段时间在支持一个i.MXRT1060客户项目时遇到了LCD ...
- Java基础语法(5)-特殊流程控制语句
title: Java基础语法(5)-特殊流程控制语句 blog: CSDN data: Java学习路线及视频 1.嵌套循环结构 将一个循环放在另一个循环体内,就形成了嵌套循环.其中,for ,wh ...
- JSON字符串带BOM头"ufeff"
调用三方接口返回值JSON字符串带BOM头"\ufeff",JSON解析死活报错. 我是用SpringBoot的RestTemplate调用三方接口的,一开始返回值我是用对象接收返 ...
- GB2312,GBK和UTF-8的区别
GBK GBK包含全部中文字符, GBK的文字编码是双字节来表示的,即不论中.英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1.至于UTF-8编码则是用以解决国际上字符的一种多字节编码 ...
- 如何实现浏览器的Console功能
离 JS-Encoder 的最初版本发布已经过了大半年的时间,这段时间除了偶尔修复一下 BUG 外,主要还是忙于学业.最近一段时间不太平,开学时间也大大延迟,加上自己本身对自己的在线编译器不是很满意, ...
- html ajax 异步加载 局部刷新
1. getJSON 优点: 简单高效,直接返回处理好的json数据 缺点: 只能使用get请求和使用json数据 <script src ='jquery.min.js'></sc ...
- 记录---java中jsp页面引入jquery路径的问题
今天在jsp页面中引入jquery的时候因为路径不对总是报404,网上的方法找到几种试了试但是最后结果还是不生效,遂想起原先的项目中有引入外部jquery的例子,所以立马看了看,发现当时的项目中是用$ ...
- Kitty-Cloud环境准备
项目地址 https://github.com/yinjihuan/kitty-cloud 开发工具 开发工具目前对应的都是我本机的一些工具,大家可以根据自己平时的习惯选择对应的工具即可. 工具 说明 ...