AJ分享,必须精品

先看效果图



源代码

NYViewController的代码



#import "NYViewController.h"
#import "NYHero.h"
@interface NYViewController () <UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSArray *heros;
@end @implementation NYViewController -(NSArray *)heros
{
if (_heros == nil)_heros = [NYHero heros];
return _heros;
} /**懒加载tableView */
-(UITableView *)tableView
{
if (_tableView == nil) {
//表格控件在创建时必须指定样式
// UITableViewStylePlain 平板格式
// UITableViewStyleGrouped分组格式
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; //添加数据源->加入协议
_tableView.dataSource = self; /*设置行高方法有两种,代理方法的优先级比setRowHeight的优先级高。
应用场景,很多应用程序,每一行高度是不一样的,例如:新浪微博
*/
// _tableView.rowHeight = 80;//第一种
_tableView.delegate = self;//第二种,要设置代理,-》协议 -》实现代理方法 [self.view addSubview:_tableView]; }
return _tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tableView];
} #pragma mark - 数据源方法 /**每个分组中的数据总数*/
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
} /**告诉表格,每个单元格的明细*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ /*
UITableViewCellStyleDefault 默认类型 标题+可选图像
UITableViewCellStyleValue1 标题+明细+图像
UITableViewCellStyleValue2 不显示图像,标题+明细
UITableViewCellStyleSubtitle 标题+明细+图像
*/
NSLog(@"表格行明细 %d",indexPath.row); //static 静态变量,能够保证系统为变量在内存中只分配一次内存空间
//静态变量,一旦创建,就不会被释放,只有在应用程序在销毁是,才会释放。
static NSString *ID = @"cell"; //1,去缓存池查找可重复用得单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //2,如果没找到
if (cell == nil) { // NSLog(@"实例化单元格");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} //设置单元格内容
// 取出英雄对象
NYHero *hero = self.heros[indexPath.row]; //设置标题 :英雄名字
cell.textLabel.text = hero.name;
//设置详细内容: 英雄描述
cell.detailTextLabel.text = hero.intro;
//设置英雄图标
cell.imageView.image = [UIImage imageNamed:hero.icon]; // 设置右边的箭头
// 1> UITableViewCellAccessoryDisclosureIndicator 箭头,可以"提示"用户,当前行是可以点击的,通常选中行,会跳到新的页面
// 2> UITableViewCellAccessoryCheckmark 对号,通常提示用户该行数据设置完毕,使用的比较少
// 3> UITableViewCellAccessoryDetailButton 按钮,通常点击按钮可以做独立的操作,例如alertView
// 点击按钮并不会选中行
// 4> UITableViewCellAccessoryDetailDisclosureButton 按钮+箭头,各自操作
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 指定右侧的自定义视图
/**
通常accessoryType提供的类型不能满足时,才会使用自定义控件 但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中!!! 自定义控件的事件触发,同样不会影响表格行的选中!
*/ // UISwitch *switcher = [[UISwitch alloc] init];
// //添加监听方法
// [switcher addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
//
// cell.accessoryView = switcher; return cell;
} -(void)switchChanged:(UISwitch *) sender
{
NSLog(@"%s %@", __func__, sender);
} #pragma mark - 实现代理方法 (行高设置)
/**设置行高,比setRowHeight优先级高*/
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
} // 取消选中某一行,极少用,极容易出错!
// didDeselectRowAtIndexPath
// didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %@", __func__, indexPath);
} /**选中了某一行*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %@",__func__, indexPath); } /**
accessoryType为按钮时,UITableViewCellAccessoryDetailButton点击右侧按钮的监听方法
此方法不会触发行选中,跟行选中各自独立
只是为accessoryType服务,对自定义控件不响应 */
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %@", __func__, indexPath);
} @end

模型的代码的代码



#import <Foundation/Foundation.h>

@interface NYHero : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *intro; -(instancetype) initWithDict:(NSDictionary *)dict;
+(instancetype) heroWithDict:(NSDictionary *)dict;
+(NSArray *) heros; @end

m实现文件

//
// NYHero.m
// 06 - lol英雄联盟
//
// Created by apple on 15-3-28.
// Copyright (c) 2015年 znycat. All rights reserved.
// #import "NYHero.h" @implementation NYHero
-(instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} +(instancetype)heroWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} +(NSArray *)heros
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
// 这才是应该写的
[arrayM addObject:[self heroWithDict:dict]];
}
return arrayM;
}
@end

——关于字典模型的类方法

AJ今天犯二了,字典初始化方法中又一个竟然这么写了,不多说了,大家看看引以为见吧

@implementation NYHero
-(instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} +(instancetype)heroWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} +(NSArray *)heros
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
//这里我原来是这么写的(注释的是错误的)
//arrayM =[self heroWithDict:dict];
// 这才是应该写的
[arrayM addObject:[self heroWithDict:dict]]; }

这二犯的,关键用debug你看的话,他会有内容,但是因为名称差不太多,应该是heros返回值是一堆对象组成的数组,结果你NSLog就发现,他丫的就返回一个地址值,很奇怪,开始我还以为是xcode大姨妈来了呢。。。事实证明,代码里面无秘密啊。
——————话说这好像是AJ写的代码中注释最少的耶。。。

代理模式阶段性小结

监听控件的某些事件
使用代理模式,是为了在程序直接“解耦”。

表格可以显示非常丰富的数据,为了达到这一结果,设置表格的“数据源”。
@required 必须实现的方法。
@optional 可选的实现方法->不强求实现->如果实现了能得到特殊的效果,如果不实现,也不影响程序的正常运行——能够增加控件的灵活度。


代理阶段性小结:(怎么用)
1,遵守协议,预先定义好方法,不实现,具体的实现工作由代理负责。
<控件的名字+DataSource> 定义的与数据有关的方法。
<控件的名字+Delegate> 定义的与事件有关的方法(通常用来监听控件事件)。

2,代理方法:
1> 方法名以控件名称开头(没有类前缀) -> 方便程序员书写的时候,快速找到需要的协议方法。
2> 第一个参数是自己 -> 意味着在协议方法中,可以直接访问对象的属性,或调用方法。
3> 代理方法的返回值 -> 控制器向控件(委托)发送数据 。


cell——UITableViewCell的注意点

缓存池的运用(老板本,后面会有新的东西更新,但是需要了解,看别人程序时候别不认识了——还有方便理解)


//1,去缓存池查找可重复用得单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //2,如果没找到
if (cell == nil) { // NSLog(@"实例化单元格");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//3设置单元格内容
//4return cell

初始化的时候需要指定cell的样式
UITableViewCellStyleDefault 默认类型 标题+可选图像
UITableViewCellStyleValue1 标题+明细+图像
UITableViewCellStyleValue2 不显示图像,标题+明细
UITableViewCellStyleSubtitle 标题+明细+图像

用法:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

指定cell右侧的视图

设置右边的箭头
1> UITableViewCellAccessoryDisclosureIndicator 箭头,可以”提示”用户,当前行是可以点击的,通常选中行,会跳到新的页面
2> UITableViewCellAccessoryCheckmark 对号,通常提示用户该行数据设置完毕,使用的比较少
3> UITableViewCellAccessoryDetailButton 按钮,通常点击按钮可以做独立的操作,例如alertView

   点击按钮并不会选中行

4> UITableViewCellAccessoryDetailDisclosureButton 按钮+箭头,各自操作
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

指定右侧的自定义视图(例:选择按钮)

 通常accessoryType提供的类型不能满足时,才会使用自定义控件

 但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中!!!

 自定义控件的事件触发,同样不会影响表格行的选中!
    UISwitch *switcher = [[UISwitch alloc] init];
//添加监听方法
[switcher addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; cell.accessoryView = switcher;

AJ学IOS(12)UI之UITableView学习(上)LOL英雄联盟练习的更多相关文章

  1. AJ学IOS 之二维码学习,快速打开相机读取二维码

    AJ分享,必须精品 上一篇文章写了怎么生成二维码,这儿就说说怎么读取吧,反正也很简单,iOS封装的太强大了 步骤呢就是这样: 读取二维码需要导入AVFoundation框架#import <AV ...

  2. AJ学IOS 之二维码学习,快速生成二维码

    AJ分享,必须精品 二维码是一项项目中可能会用到的,iOS打开相机索取二维码的速度可不是Android能比的...(Android扫描二维码要来回来回晃...) 简单不多说,如何把一段资料(网址呀,字 ...

  3. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  4. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  5. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

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

    AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...

  7. AJ学IOS(28)UI之Quartz2D简单介绍

    AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...

  8. AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController

    AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...

  9. AJ学IOS(56)网络基础以及如何搭建服务器

    AJ分享,必须精品 一:为什么要学习网络编程 关于这个问题,为什么要学习网络编程,AJ的理解就是,这东西是时代发展的必要,没什么为什么,就是应该学,除非你就是想玩单机,但是就算是单机也会有购买金币之类 ...

随机推荐

  1. 从零开始学习R语言(三)——数据结构之“矩阵(Matrix)”

    本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/60140022 也同步更新于我的个人博客:https://www.nickwu.cn/blog/id=129 3. [二 ...

  2. Cisco 综合配置(二)

    要求: 1. PC1 属于VLAN10,PC2属于VLAN20,网关:Master Router2. VLAN10.20 的网段为:192.168.10.0/24 . 192.168.20.0/24 ...

  3. JDBC(三)----JDBC控制事务

    ##  JDBC控制事务 1.事务:一个包含多个步骤的业务操作.如果这个业务员操作被事务管理,则这多个步骤要么同时成功,要么同时失败. 2.操作: 1.开启事务 2.提交事务 3.回滚事务 3.使用C ...

  4. windows10 64位 安装mysql服务端 并使用navicat客户端链接 掉的一堆坑

    1.目的 安装mysql服务端 并使用navicat客户端链接 2.过程 1)下载mysql服务端 下载过程(参考https://blog.csdn.net/youxianzide/article/d ...

  5. junit Mockito使用入门

    junit Mockito使用入门 准备 在我们进一步讨论之前,让我们探索几种不同的方法来启用Mockito测试中注释的使用. 方式一 MockitoJUnitRunner 我们拥有的第一个选择是使用 ...

  6. Prism 源码解读4-ViewModel注入

    介绍 介绍一个Prism的MVVM实现,主要介绍Prism如何在WPF上进行的一些封装,以实现MVVM.MVVM到底是什么呢?看一下这一幅经典的图 以前没有ViewModel这个概念,就是将Model ...

  7. Flume数据采集结合etcd作为配置中心在爬虫数据采集处理中的架构实践。

    Apache Flume是一个分布式的.可靠的.可用的系统,用于有效地收集. 聚合和将大量日志数据从许多不同的源移动到一个集中的数据存储,但是其本身是以本地properties作为配置的,配置无法做到 ...

  8. Web安全认证

    一.HTTP Basic Auth 每次请求 API 时都提供用户的 username 和 password. Basic Auth 是配合 RESTful API 使用的最简单的认证方式,只需提供用 ...

  9. html ajax 异步加载 局部刷新

    1. getJSON 优点: 简单高效,直接返回处理好的json数据 缺点: 只能使用get请求和使用json数据 <script src ='jquery.min.js'></sc ...

  10. win10 安装redis相关问题。

    最近需要在win10安装redis,但是redis的msi文件总是报这个错误: Redis on Windows Setup Wizard ended prematurely 都说是.NET fram ...