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. [BJDCTF 2nd]old-hack

    进入首页: 首页告诉了我们是thinkphp5的漏洞. 知道了是哪个版本的话就搜一搜喽:最后发现是thinkphp5.0.23的命令执行 payload_1:查看根目录文件,发现flag位置 http ...

  2. echar图柱状图和折线图混合加双侧y轴坐

    代码如下: floorSalesBar(){//方法名====这个方法应该放在methods中并在mounted中调用哦 methods let _this = this; let myChart = ...

  3. Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails

    现象 MySQL在删除一张表时出现 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint ...

  4. [Docker03] Deploy LNMP on Docker

    Deploy MYSQL docker pull mysql 挂载卷保存数据文件 mkdir -p /mysql/data chmod -p 777 /mysql/data MySQL使用过程中的环境 ...

  5. OpenAI的GPT-2:用Python构建世界上最先进的文本生成器的简单指南

    介绍 "The world's best economies are directly linked to a culture of encouragement and positive f ...

  6. HDU - 3791 建立二叉搜索树

    题意: 给定一个序列,下面又有n个序列,判断这个序列和其他序列是否为同一个二叉树(同一序列数字各不相同) 思路: 首先讲将一个序列建立成二叉搜索树,然后将其他序列也建立二叉搜索树,两个树进行前序遍历, ...

  7. Python math库和random库

    1.math库 >>> from math import * >>> 2*pi 6.283185307179586 >>> e 2.7182818 ...

  8. Sql练习201908200916

    表结构: 一条sql修改多条数据(Sql server),oracle,mysql请自行尝试: ; go 完成.

  9. Java String与char

    1. char类型 + char 类型 = 字符对应的ASCII码值相加(数字): char类型 + String 类型 = 字符对应的ASCII码值相加(数字) + String 类型: Strin ...

  10. 【WPF学习】第六十六章 支持可视化状态

    上一章介绍的ColorPicker控件,是控件设计的最好示例.因为其行为和可视化外观是精心分离的,所以其他设计人员可开发动态改变其外观的新模板. ColorPicker控件如此简单的一个原因是不涉及状 ...