以前iOS的笔记---纯手打(1)

*7月8日上午

N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block或__weak给其重新命名一遍

写的方法 (1)Bolck 的实现:返回值(^名字)(形式参数……)=^返回值(实参……){代码,有返回值的话就返回};

(2)Block的回调:名字(需要导入的实参……);

7月8日下午

在类里的#improt”类名”下创建Block

typded返回值(^代号)(形式参数……);

将代号变为属性并重新命名{ 代号 *任意名字}

并在方法中建立属性的block[需要重新命名,也可以用原始的代号创建]

7月11日上午

破坏封装性—KVO的建立:(1)要使被观察属性的值产生变化

(2)创建监视者:[被检查者 addObserver:监察者 forKeyPath:@“被检查者的属性” options:NSKeyValueObservingOptionNew(Old) context:nil];

(3)监视者方法-o开头的:-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

NSLog(@"da");

if ([[change objectForKey:@"new"]isEqualToString:@"60"]) {

_children.hunger=100;

}

}

(4)销毁监视的属型[lei属性 removeObserver:self forKey:@“被检查的属性” context:nil];

KVO与委托模式的相同点:都可以进行事件的通信,数值的传递,

不同点:KVO是一对多,委托是一对一的

N:一般采用—通知:(1)创建通知中心:[[NSNotificationCenter defaultCenter]postNotificationName:发出的属性 object:self userInfo:添加的数据];

(2)接收通知中心的属性:[[NSNotificationCenter defaultCenter]addObseerver:self selector:@selector(调用的方法:) name:@“接受的属性” object:nil];

(3)通知中心调用的通知方法:【是通知不是通知中心】:-(void)方法名:(NSNotification *)名字{

名字.usinfo;中取值

};

(4)销毁通知:在dealloc方法里[[NSNotificationCenter defaultCenter]removeObserver:self name:@“要销户的属性” object:nil];

7月11日下午


N:谓词:提供了一个NSPredicate类,指定过滤器的条件。将符合条件的对象保留下来,可以来取出满足条件的对象;

条件:就是属性—>

*a—>最后一个符=a的;

a*—>第一个=a;

*a*—>中间有一个a;

*a?—>倒数2个是a;

?a—>第2个是a;

属性 in{‘属性名’}—>取出特定属性名的对象

也可以去取或—>||,与—>,非—>!

*代表了任意的字符,?代表一个字符;

也可以通过<,>,=来取出符合条件的对象;

、事例—>

NSPerdicate *perdicate=[NSPerdicate perdicateWithFormat:@“条件”];

NSArray *arr=[总数组 filteredArrayUsingPredicate:perdicate];

for(Person *p in arr){

NSLog(@“%@”,p);

}

7月12日上午

N:时间

NSDate *date=[NSDate date];得出来的是格林尼治时间 0时区

NSTimerInterval time1970(只是名字)=[date timeIntervalSince1970];时间戳:通过时间戳(timeIntervalSince1970)来获得现在到1970年的秒数

也可以通过时间戳的秒数来加上需要的秒数得到时间NSDate=[NSDate dateWithTimeIntervalSinceNow:8 * 60 *60];

格式化日期——>NSDateFormatter *f=[[NSDateFormatter alloc]init];

[f setDateFormatter:@“yyyy年M月d日 H:m:ss”];

NSString *s=[f stringFormDate:date];

NSLog(@“%@”,s);

N:沙盒:一个App里只能有一个沙盒,沙盒只能允许唯一的App访问,保障了用户的安全;

沙盒具有3个文件:(1)Library:用于保存系统文件,(2)Documents:存放永久文件,如备份等;(3)Tmp:存放临时文件,重启App后清空!

N:获取沙盒路径:NSString *s=NSHomerDirectory();

7月12日下午

N:字符串路径:NSString *path=@“/User/whbe/Desktop/ADP-187.torrent”;

NSArray *arr=[path pathComponents];pathComponents用于获取每一步的路径

NSString *Lastpath=[path lastPathComponents];获取最后的路径

N拼接路径(追加文件)

追加文件扩展名:NSString *newpath2=[newpath stringByAppendingPathExtension@“”]

删除最后文件:NSString *newpath3=[newpath2 stringByDeletingLastPathComponent];

删除文件扩展名:NSString *newpath4=[newpath2 stringByDeletingPathExtension];

N:文件管理:用于创建或删除文件的;

创建文件管理器(具有单例,也就是唯一性):NSFileManager *manager=[NSFileManager defaulitManager];

创建路径地址:NSString *pata=[NSHomDirectory stringByAppendingPathComponent:@“路径”];

创建文件的字符:NSString *neirong=@“内容”;

创建内容:NSData *data=[neirong dataUsingEncoding:NSUTF8StringEncoding(UTF8的字符)];

创建文件:BOOL is1=[manager createFileAtpath:pata(地址路径) contents:data(内容) attributes:nil];

检验是否创建:NSLog(@“%d”,is1);

创建复制的路径地址:NSString *pata2=[NSHomDirectory stringByAppendingPathComponent:@“路径”];

复制文件:BOOL is2=[manager copyltemAtPath:path(原地址) toPath:pata2(需要复制到的地址) error:nil];

检验是否复制:NSLog(@“%d”,is2);

创建剪切的路径地址:NSString *pata3=[NSHomDirectory stringByAppendingPathComponent:@“路径”];

剪切文件:BOOL is3=[manager moveitemAtPath:pata2(剪切前的) toPath:pata3(剪切后的) error:nil];

接受文件的内容:(1)NSData *data2=[manager contentsAtPath:pata3];从文件管理器中接受

(2)NSString *ner=[[NSString alloc]initWithData:data2(接受的内容) encoding:NSUTF8StringEncoding(转变代码 )];

查找文件是否存在:BOOL is4=[manager fileExistsAtPath:pata3(文件地址)];

删除文件:BOOL is5=[manager removeltemAtPath:path3(删除文件的地址) error:nil];

7月13日上午

N:单例:1.单例主要适用于存储一些公共的数据,每一个对象都可以进行共享访问、修改2.如果有类的对象的创建非常消耗性能,这样的类也可以设计成单例类,只创建一次对象来节省性能

例如:通知\文件管理器都是单例,只能有一个存在,就算再创建一个,还是原来的那一个;

单例的创建:(1)在类里创建方法(以+(instancetype)share……;为例!)

(2)方法的实现:【1】在方法外创建以 static 类名 *名字=nil;在方法里 if(名字==nil){

名字=[[self alloc]init];

return 名字;

}

(3)在main.h文件里用类方法创建;

单例的创建:+ (instancetype)shareButton {

static ZAButton * btn = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

btn = [[self alloc] init];

});

return btn;

}

7月14日下午

如何从数据中获取信息:注:在这里所有的引用都是强引用Strong,不是Weak(1)在xcode中创建一个空白的文件,将文件内容复制粘贴

(2)写出在程序中写出文件的路径 :NSString *path=[[NsBundle mainBundle]pathForResource:@“路径” ofType:nil];

(3)创建内容:NSData *data=[NSData dataWithContentsOfFile: path(文件名)];

(4)从文件中获取字典[解析.json文件]:NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:data(获取内容)options:NSJSONReadingMutableContainers error:nil];

(5)获取字典中的数组:NSArray *resultArr=resultDic[@“键”];

(6)创建一个强引用的可变数组来接受对象的类并先给强引用的数组内赋给个数:#import "Model.h"

@interface ViewController ()

@property(nonatomic,strong)NSMutableArray *dataArr;

@end

self.dataArr = [NSMutableArray arrayWithCapacity:resultArr.count];

(7)遍历resultArr中的字典取出数组中的字典,输入键为你所需要的字典取出字典,将字典构造成对象,并将对象放入dataArr里:for (NSDictionary *contentDic in resultArr) {

//取出所有的subject字典

NSDictionary *subjectDic = contentDic[@"subject"];

//将字典构造成了Model对象

Model *m1 = [[Model alloc] initWithDictionary:subjectDic];

//将所有的Model对象放入dataArr

[self.dataArr addObject:m1];

}

(8)从可变数组_dataArr中取出你所需要的信息;

全文如下:

ViewController.h文件

#import "ViewController.h"

#import "Model.h"

@interface ViewController ()

@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

NSString *path = [[NSBundle mainBundle] pathForResource:@"su_.json" ofType:nil];

//2.读取JSON文件,序列化JSON的数据容器

NSData *data = [NSData dataWithContentsOfFile:path];

NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSLog(@"resultDic = %@", resultDic);

//3.获取到了subjects数组

NSArray *resultArr = resultDic[@"subjects"];

self.dataArr = [NSMutableArray arrayWithCapacity:resultArr.count];

//遍历resultArr数组的字典

for (NSDictionary *contentDic in resultArr) {

//取出所有的subject字典

NSDictionary *subjectDic = contentDic[@"subject"];

//将字典构造成了Model对象

Model *m1 = [[Model alloc] initWithDictionary:subjectDic];

//将所有的Model对象放入dataArr

[self.dataArr addObject:m1];

}

NSLog(@"%@",_dataArr);

//从Model里面取出数据

Model *m2 = _dataArr[0];

NSString *title = m2.title;

NSLog(@"%@", title);

for (Model *m in _dataArr) {

NSLog(@"%@",m.title);

}

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

Model.h文件

#import

@interface Model : NSObject

@property(nonatomic,strong)NSString *title;

@property(nonatomic,strong)NSString *year;

@property(nonatomic,strong)NSDictionary *images;

-(instancetype)initWithDictionary:(NSDictionary *)dic;

@end

Model.m文件

#import "Model.h"

@implementation Model

-(instancetype)initWithDictionary:(NSDictionary *)dic{

self=[super init];

if (self) {

self.year=dic[@"year"];

self.title=dic[@"title"];

self.images=dic[@"images"];

}

return self;

}

@end

7月17日下午

UI第一课

UIUserInterface(用户界面)

1.iOS系统版本,每年都有更新.对我们开发者而言,主要的是观察API的变化.

2.iPhone新手机发布,会产生不同尺寸的屏幕,现在市面上有4种尺寸,我们需要考虑屏幕适配问题.

3.iOS系统层级,分为4层.目前我们学习的就是最顶层Cocoa touch层(layer),我们使用的是UIKit框架

4.iOS SDK(软件开发工具包).iOS开发语言OC,Swift——都是面向对象.

5.我们最简单的创建UI项目的方法就是通过storyboard(故事版)来完成.

6.我们可以通过设置故事板上面的控件属性(右边栏,第四个按钮——属性设置)

7.

—模拟器的输入模式切换:command+shift+K

—模拟器大小的切换:command+1,2,3

—模拟器锁屏:command+L

—模拟器Home键:command+shift+H

infoQ网站

www.stackOverFlow.com

github.com

cocoaChina.com

code4app.com

编程查资料最好用Google

素材

http://www.58pic.com/shiliangtu/14288360.html

http://588ku.com/beijing/0-29-dnum-0-5/

7月19日

1.与其配套的合成app图标的软件是Prepo;

2.今天讲的:(1)在苹果手机里,坐标以左上角为原点;

(2)在文件创建UILable,并且设定位置: UILable *lable=[UILable alloc]initWithFrame:CGRectMake(X,Y,视图的宽width,视图的高height);

3.程序的生命周期AppDelegate.m方法:(1)程序完成启动,- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions—创建Xcode工程选中singleView模板,并且做好相应设置,设置好主窗口,和主窗口的根控制器。相当于该方法隐藏的设置好了上述。;

(2)程序将要进入前台- (void)applicationWillEnterForeground:(UIApplication *)application

(3)程序已经变为活跃:-(void)applicationDidBecomeActive:(UIApplication *)application —-1.在程序启动的时候会调用一次。2.从后台回到前台3.从任务管理界面回到前台;

(4)程序将要辞去活跃状态(进入非活跃状态)- (void)applicationWillResignActive:(UIApplication *)application程序不再是用户可以操作的时候,进入非活跃(双击Home键)暂停游戏(在这里暂停一些任务);

(5)3.程序已经进入后台- (void)applicationDidEnterBackground:(UIApplication *)application—程序完全看不见了,进入后台。程序挂(2)程序由用户自己结束的时候,先进入后台;

(6)6.程序将要结束- (void)applicationWillTerminate:(UIApplication *)application—// 程序结束的时候 程序自己崩溃的时候如果时机合适,你可以做一些数据储存类的工作;

(7程序接受到内存警告(系统发送,如果一直占用过高,系统会直接杀掉进程)- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

NSLog(@"收到内存警告");

})

7月20日

UIwindow:是窗口,UIwindow继承于UIView,但是却可以容纳UIView

UIView:是视图的基类,UIView(父视图)里也可以许多UIView(子视图),

获取屏幕的尺寸:CGRect rect=[UIScreen mainScreen].bounds;

创建一个窗口并应用在屏幕上的步骤:(1).创建窗口self.window=[[UIwindow alloc]init];

(2).将窗口添加并设置成主要:[self.window makeKeyAndVisible];

(3).给窗口设置一个窗口管理者:self.window.rootViewController=[[ViewCentroller alloc]init];

Frame:以父视图为起点有CGRectMake(x,y,width,height);

Bounds:只有大小没有起点CGRectMake(0,0,width,height);

Center:表示视图中心点CGPiont(x,y);

7月22日

UIView:可以添加到UIWindow里—》 [self.window addSubview: View(name)];

NSBundle:是代码在运行时的一个包,里面包含了你所需要的数据(图片)

如何添加View.xib视图在window里和获取NSBunble里的数据:(1).创建一个NSBunble:NSBunble *bubDle=[NSBunble mainBunble];

(2).用数组来接受NSBunble中的东西:NSArray *DArray=[bunDle loadNiNamed:@“文件的名字”owner:self(处于的文件) options:nil(添加的内容)];

(3).创建个UIView用于接收数组中的View:UIView *View=DArray[0];

(4).将View添加到window里:[self.window addSubview:View];

杂项:(1).tag值:是一个标签,代表了拥有这个标签的View或其他的东西;(也可以用来或取【1】UIView *Aview=[self.view viewWithTag:tag的值])

(2)hidden:是一个视图是否隐藏;(view.hidden=YES或NO,默认的是NO);

(3)alpha:是一个视图的透明度;(view.alpha=1;默认是1,可自行修改);

(4)UIbutton里面无论是设置什么都是用set开头

(5)当用通知或别的东西传递类与类之间的数据时,应观察视图是否已经构建(是构建不是创建)好!

注:•实现一个单例

//开头为share或者default

+ (instancetype)shareViewController {

//在初始化时,将vc设为nil

static ViewController * vc = nil;

//判断vc是否存在,决定是否进行初始化操作,因为vc使用static修饰,所以vc == nil判断只会成立一次

if (vc == nil) {

vc = [[ViewController alloc] init];

}

return vc;

}

简述导航控制器内部实现机制

导航控制器是一个视图控制器的管家,并不负责进行界面的显示,只对所有的子控制器进行管理。导航控制器在进行push和pop操作时,push是将控制器放入导航控制器中进行管理,pop是讲控制器移出导航控制器,并且进行释放。在栈模型中,分别对应入栈(压栈)和出栈(弹栈)操作。一般来说导航控制器和标签栏控制器还有视图控制器共同组成三级控制器结构,导航控制器作为标签栏控制器的视图控制器之一,视图控制器作为导航栏控制器的根视图控制器

简述UITableViewCell的复用机制

UITableViewCell使用复用时有两种方法,两种方法都是在需求单元格时去复用池中寻找相同类型(通过复用Identifier)的单元格,如果单元格在复用池有存在一个或多个,就可以直接取出进行复用。如果没有,方法内部会自动进行创建。在使用新的复用方法时,一定要先注册单元格,才能够进行复用。

7月23日

图片导入:(1)UIImageView *imageView=[[UIImageView alloc]init];

(2)imageView.image=[UIImage imageNamed:@“图片的名字.格式”];

图片的拉伸方法:

-(UIImage *)outUIImage:(NSString *)Name{

UIImage *image=[UIImage imageNamed:Name];

image=[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 110, 0, 110)];(上左下右)

return image;

}

UI动画制作:

(1).旋转动画方法:[UIView animateWithDuration:1(调用时间) animation:^{View.transform=CGAffineTransformRotate(View.transform,M_PI==180*C(M_PI_2==90*C,M_PI_4==45*C转换的角度))}completion:^(BOOL finished){(当动画运行完后应该运行什么)}] ;

(2)平移动画方法:[UIView animateWithDuration:1(调用时间) animation:^{View(改变的视图后的.transform).transform=CGAffineTransformScale(View(改变的视图前的.transform).transform,输入x(横移的X),上升下降的y(上升下降的y))}completion:^(BOOL finished){(当动画运行完后应该运行什么)}] ;

(3)缩放动画方法:[UIView animateWithDuration:1(调用时间) animation:^{View.transform=CGAffineTransformScale:(View.transform,x轴缩小放大的度(比例是1:x),y轴缩小放大的度(1:y))}]

(4)回归原始形态:[UIView animateWithDuration:1 animation:^{View.transform=CGAffineTransformIdentity;}];

View颜色的多次改变:(1).建立一个颜色的可变数组,再建立一个UIView的可变数组

(2)利用for循环并且将颜色数组或UIViwe数组里的颜色或View相互交换

例:[UIView animateWithDuration:1 animations:^{

for (int u=0 ;u<7 ;u++) {

UIView *Vie=_Array[u];

Vie.backgroundColor=_colorArray[6-u];

//[_Array exchangeObjectAtIndex:0 withObjectAtIndex:u];

[_colorArray exchangeObjectAtIndex:6withObjectAtIndex:u ];

}

} completion:^(BOOL finished) {

}];

7月24日

UIlable的各种方法:(1)创建:UILabel * textLabel = [[UILabel alloc]init]];

(2)内容:textLable.text=@“内容”;

(3)行数:textLable.numbberOfLines=0;[等于o时代表不限制行数]

(4)字体的大小设置:text.font=[UIFont systemFontOfSize:字号];(也可以在Sys前bold,可变粗体)

(5)根据label宽度调整字体:textLabel.adjustFontSizeToFitWidth=YES;

(6)根据字体设置Label尺寸:[textLabel sizeToFit];

(7)设置文本阴影:textLabel.shadowColor=[UIColor redColor];颜色————textLabel.shadowOffset=CGSizeMake(5,5);—偏移

(8)UILable具有高亮模式:

(9) 让字体适应label的大小:self.label.adjustsFontSizeToFitWidth = YES;

UIImageView的各种方法:(1)创建一个UIImageView:UIImageView *imageView=[UIImageView alloc]initWithFrame:CGRectMake(尺寸);

(2)给imageView添加图片:imageView.image=[UIImage imageNamed:@“图片的名字”];

(3)设置视图的内容格式:imageView.contentMode=UIViewContentModeCenter;(UIViewContentModeScaleToFill拉伸铺满视图,不在乎图像比例

UIViewContentModeScaleAspectFit自适应拉伸,不破坏宽高比,如果宽/高沾满视图,另外一边不再进行缩放

UIViewContentModeScaleAspectFill不破坏宽高比,可以超出视图范围)

(4)裁剪图片需要设置的半径:imageView.layer.cornerRadius=150;图片的半径切割后可为圆形

(5)是否进行切割:imageView.clipsToBounds=YES;

(6)UIImageView也具有高亮模式;

连贯的图片动画制作:(1)将需要的图片添加到Xcode里。

(2)[创建一个UIImageView的属性],并且在方法里创建一次(否则UIImageView为空)@property (nonatomic, strong) UIImageView * animationView;

self.animationView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];

(3)[创建一个可变数组并将图片转化为UIImage并添加到数组]中去:NSMutableArray *imagesArray=[NSMutableArray array];

for(int i=1;i<图片个数+1;i++){

NSString *iamgeName=[NSStringstringWithFormat:@“图片的名字%d”,i];

UIImage *image=[UIImage iamgeNamed:imageName];

[imagesArray addObject:image];

}

(4)[给动画设置属性时一定要在创建视图后设置,否则无法设置];

(5)将[数组里的图片传给视图的动画数组]:self.animationView.anmationImaege=imagesArray;

(6)将animationView添加到self.view:[self.view addSubview:_animationView];

(7)设置每张图画的时常:self.animationView.animationDuration=…;默认的是0.1秒;

(8)重复次数:self.animationView.animationRepeatCount=…;默认是无限

(9)开始动画:[self.animationView starAnimation];

(10)关闭动画:[self.animationView stopAnimating];

7月25日

UIControl:是具有事件处理控件的父类;

UIControl继承与UIView所以UIControl可以添加事件,并且可以签订协议,而且还可以comder的键去取出它的方法;

UIButton的状态:(1)正常状态:UIControlStateNolmal

(2)高亮:UIControlStateHighlighted

(3)禁用:UIControlStateDisabled

(4)选中:UIControlStateSelected

UIButton的方法:(1)添加标题:[butten setTitle:@“标题名” forState:状态];

(2)添加标题的颜色:[butten setTitleColor:[UIColor redcolor] forState:状态];

(3)添加图片:[butten setImage:[UIImage imageName:@“图片名字”] forState:状态];

(4)添加背景图片:[butten setBackgroundImage:[UIImage imageName:@“名字”] forState:状态];

(5)设置标题字体:butten.titleLabel.font=[UIFont systemFontOfSize:大小];

(6)添加方法事件:[butten addTarget:self action:@selector(方法名) forControEvents:UIControlEvenTouchUpInside(触发状态)];

(7)

UITextField:文本输入框

(1)键盘类型:text.keyboardType=UIKeyBoardTypeDefault;

(2)外框风格:text.borderStyle=UITextBorderStyleRounderdRect;(UITextBorderStyleNone,UITextBorderStyleLine,UITextBorderStyleBezel,UITextBorderStyleRoundedRect)

(3)内容大小:text.font=[UIFont systemFontOfSize:大小];

(4)内容格式:text.textAlignment=NSTextAligenmentCenter;

(5)提示输入:text.placeholder=@“提示”;

(6)清除格式:text.clearButtonMode=UITextFiedViewModeUnlessEditing;(UITextFieldViewModeNever,------永远不出现,UITextFieldViewModeWhileEditing,------当编辑的时候出现,UITextFieldViewModeUnlessEditing-----当结束编辑时出现,UITextFieldViewModeAlways-----一直出现)

(7)是否支持清除:text.clearsOnbeginEditing=YES;

(8)是否支持安全输入:text.secureTextEntry=YES;

(9)设置键盘右下角的Return格式:text.returnKeyType=UIReturnKeyDone;

(10)给输入框添加动作:[text addTarget:self action:@selector(方法名) forControlEvents:UIControlEventEditingDidBegin];

(11)给输入框添加代理:text.delegate=self;

(12)text的委托方法实例:// return NO to disallow editing.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

NSLog(@"将要开始编辑");

return YES;

}

// became first responder

- (void)textFieldDidBeginEditing:(UITextField *)textField {

NSLog(@"已经开始编辑");

}

// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

NSLog(@"将要结束编辑");

return YES;

}

// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

- (void)textFieldDidEndEditing:(UITextField *)textField {

NSLog(@"已经结束编辑");

}

// return NO to not change text

// 获取输入框的状态,并且决定是否继续编辑

// 输入框将要修改的内容

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

// 新内容所在的位置---长度为0

NSLog(@"range :%@", NSStringFromRange(range));

// 新输入的内容

NSLog(@"string :%@", string);

if (range.location <= 10) {

return YES;

}

// 如果返回NO的话,字符内容不发生变化。依然会接受

return NO;

}

// called when clear button pressed. return NO to ignore (no notifications)

- (BOOL)textFieldShouldClear:(UITextField *)textField {

return YES;

}

// called when 'return' key pressed. return NO to ignore.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

// 模拟应用效果

// 用户点击返回按钮,收起键盘/发送消息

[textField resignFirstResponder];

NSLog(@"%@", textField.text);

return YES;

}

7月26日

UISlider:是滑块

(1)滑块的最大值:slider.maximumValue=1000;

(2)滑块的最小值:slider.minimumValue=0;

(3)滑块的Value:是可以直接修改滑动的进度条

(4)可以给滑块的左右和滑块设置图片或颜色:[slider setMinimumTrackTintColor:[UIColor greenColor]];

[slider setThumbImage:[UIImage imageNamed:@"playing_slider_thumb@2x"] forState:UIControlStateNormal];

(5)也可以添加方法!

8月01日

UIViewController:视图控制器

视图不是直接添加在window视图,而是添加在根视图控制器上(rootViewController)

(1)如何添加视图控制器:方法:(1)创建一个继承与UIViewController的类并且点击创建页面的(Also create XIB file)按钮,导入到AppDelegate.m文件,并初始化然后赋予self.window.rootViewController

方法:(2)创建一个继承与UIViewController的类,并创建一个View文件,将文件的与类绑定身份并且将(1).选中file's owner ,身份设置为指定的控制器

*(2).连线到view视图上

拟态视图:拟态视图不能自己显示到视图上,只能通过按钮,拟态视图关闭后就会被程序清除内存,不会保存原来的数据

(1)创建一个继承与UIViewController的类,并创建一个按钮给其添加方法

(2)在按钮的方法中,类进行init: ZAViewController *za=[[ZAViewController alloc]init];

(3)并且进行构建视图:[self presentViewController:za( 类名的简称)animated:YES(是否有动画) completion:^{创建完视图后回调什么}];

关闭拟态视图:通过按钮来关闭:(1)[self dismissViewVontrolerAnimated:YES completion:^{}];

// UIModalTransitionStyleCoverVertical垂直弹出

//UIModalTransitionStyleFlipHorizontal水平翻转

//UIModalTransitionStyleCrossDissolve渐入渐出

//UIModalTransitionStylePartialCurl竖直翻页

拟态视图的模式:modalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;注:写在拟态视图创建前

8月02日

在UI模式下数据的传输:(1)基本的数据传输:①.在Main.stroyboard中创建两个View,一个是主一个是辅

②.UIStroyboard的创建: UIStroyboard *sb=[[UIStroyboard alloc]init];

③.取出辅视图:UIViewController * Vie=[sb instantiateViewControllerWithIdentifier:(辅视图的id)];

④.进行构建视图:[self presentViewController:Vie(视图控制器)animated:YES(是否有动画) completion:^{创建完视图后回调什么}];注:也可以通过UIStroyboad获取各种视图

(2)代理:①创建一个协议:@protocol ZAWdelegate 创建协议

-(void)stringWithText:(NSString *)string;协议方法

@end,

②.创建一个协议的属性:@property(nonatomic,strong)iddelegate;

③.让另一个类签订协议

④.给签协议的那个类写协议方法:-(void)stringWithText:(NSString *)string{self.textField.text=string;}想让数据传输到哪就给哪个赋值

⑤.在需要传输的时候调用:[self dismissViewControllerAnimated:YES completion:^{

[_delegate stringWithText:self.textField.text];}];

⑥.给协议的属性给予目标:ZAViewController *za=[[ZAViewController alloc]initWithDelegate:self];

注:-(id)initWithDelegate:(id)delegate{

if (self=[super init]) {

_delegate=delegate;

}

return self;

}

(3)单例:

①.创建一个单例:+(instancetype)shareDanLi{

static ViewController *view=nil;

if (view ==nil) {

view =[[self alloc]init];

}

return view;

}

②.在AppDelegate.m文件里创建新的Window,并且self.window.rootViewController=[ViewController shareDanLi];注:为了防止self.Window访问Main.stroyborad

③.在ZAViewController里创建一个ViewController属性,并且在需要的时候-传递数据(void)tuse:(UIButton *)bu{

self.ViewC=[ViewController shareDanLi];

[self dismissViewControllerAnimated:YES completion:^{

self.ViewC.textField.text=self.textField.text;

}];

}

(4)通知:

①.创建一个通知并添加上需要传递的数据:[[NSNotificationCenter defaultCenter]postNotificationName:@"textField" object:self.textField.text userInfo:nil];

②.接受通知在viewDidLoad的方法里:[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tyse:) name:@"textField" object:nil];

③.通知方法并从object中取出想要的数据然后赋给想要的东西:

-(void)tyse:(NSNotification *)te{

self.textField.text=te.object;

}

(5)block.看准时机进行block的回调

8月03日

导航视图控制器:(UINavigationController)可控制多个视图控制器

导航视图控制器的创建:(1).给窗口的根视图控制器设置一个导航视图控制器并赋予导航控制器的根视图控制器一个视图:self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];

(2).导航视图控制器只能由按钮弹出!!

(3).给导航视图控制器压栈(去往下一层):[self.navigationController pushViewController:(继承于UIViewController的类)[[ZAViewController alloc]init] animated:YES(是否有动画)];

(4).给导航视图控制器弹栈(返回上一层):[self.navigationController popViowControllerAnimated:YES];

(5).给导航视图控制器弹栈回到根视图:[self.navigationController popToRootViewControllerAnimated:YES];

(6).给导航视图控制器跳页:①.创建一个弹窗控制器:UIAlertController *AlertController=[UIAlertController alertControllerWithTitle:@“标题” message:@“内容” preferredStyle:UIAlertControllerStyleAlert(UIAlertControllerStyleActionSheet)];

②.给弹窗控制器添加文本:[AlertController addTextFieldWithConfigurationHandler:nil];

③.给弹窗控制器设置按钮并实现导航控制器的跳页:UIAlerAction *alert=[UIAlerAction actionWithTitle:@“按钮上的字” style:UIAlertActionStyleDefault(按钮的模式UIAlertActionStyleCancel,UIAlertActionStyleDestructive)handler:^(UIAlerAction *_NonnullAction){(点击后运行的东西,可为nil)

NSArray *arr=[AlertController textFields];取出弹窗控制器里的文本框

UITextField *textField=arr[0];取出数组中的文本框

NSString *string=textField.text;取出文本框中的内容

int a=[string intValue];将字符串改为数字

if(a0){

UIViewController *view=self.navigation.viewControllers[- -a];取出视图

[self.navigationController popToViewController:(需要跳转的页面)view animated:YES];

}];

④.将弹窗控制器添加到self.view上:[AlertController addAction:alert];

[self.view presentViewController:AlertController animated:YES completion:nil];

8月05日

导航控制栏的设置(navigationBar):(1)设置导航栏的风格:self.navigationController.navigationBar.barStyle=UIBarStryleBlack;

(2)设置导航栏为透明(或是给导航栏是否整张图片拉伸):self.navigationController.navigationBar.translucent=YES;

(3)为导航栏子控件设置颜色:self.navigationController.navigationBar.tiintColor=[UIColor redColor];

(4)给导航栏设置图片:[self.navigationController.navigarionBar setBackgroundImage:[图片] forBarMetrics:UIBarMetricsDefeult];

(5)在TableView里将导航栏设置为不透明:self.edgesForExtendedLayout = UIRectEdgeNone

// 获取导航栏

UINavigationBar * bar = self.navigationController.navigationBar;

// 使用backgroundColor设置没有效果

//bar.backgroundColor = [UIColor blackColor];

// 设置导航栏的背景颜色

bar.barTintColor = [UIColor magentaColor];

// tintColor设置导航栏上子控件的默认内容颜色

bar.tintColor = [UIColor whiteColor];

//给导航栏设置隐藏

self.navigationBarHidden = YES;

//给navigation上的title赋予颜色大小

[self.navigationController.navigationBar setTitleTextAttributes:

@{NSFontAttributeName:[UIFont systemFontOfSize:23.f],

NSForegroundColorAttributeName:[UIColor whiteColor]}];

也可以就是不能设大小

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//从story中取出ViewController并从UIApplication中取出UITabBarController中取出navigationController并弹出视图

NewTableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];

if ([cell.reuseIdentifier isEqualToString:@"imageCell"]) {

}else{

UIStoryboard *story = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

NewWedAndViewController *newWed =[story instantiateViewControllerWithIdentifier:@"NewWedAndViewController"];

UITabBarController *bar = [UIApplication sharedApplication].keyWindow.rootViewController;

UINavigationController *navi = bar.viewControllers[0];

[navi pushViewController:newWed animated:YES];

navi.navigationBarHidden = YES;

8月13日

(1).在纯故事版模式中使用:prepareForSeguse可直接获取到push时的View:在纯使用StoryBoard(使用线)时,使用导航栏或UIBatton进行(连线另一个视图)pushViewController时,不用给UIBatton添加方法可以直接用系统的方法(-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender{

通过segue可以取到连线的目标控制器

ZASecondViewController * vc = segue.destinationViewController;

然后可以随时访问完vc.view后传递数据

})

(2).在使用一个关联在一起的几个(例.两个Label一个Button)控件时,应采用创建一个继承与UIControl的类【UIControl 可以自己添加方法,在UIControl中没有viewdidload,所以要初始化(-(instancetype)initWithFrame:(Cgrect)frame{})添加UILabel时要以自己的尺寸为父视图,给自己添加方法时要用self】

例:在另一个类里创建时:ZAControl * control = [[ZAControl alloc] initWithFrame:CGRectMake(0, 100, kScreenWidth, 40)];

[self.view addSubview:control];

例:- (instancetype)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]){

self.backgroundColor = [UIColor whiteColor];

UILabel * nickName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, frame.size.height)];

nickName.text = @"昵称";

[self addSubview:nickName];

UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, kScreenWidth - 40, frame.size.height)];

name.textAlignment = NSTextAlignmentRight;

name.text = @"哇哈哈";

name.tag = 1001;

给自己的添加东西

[self addSubview:name];

// 直接在control对象上添加事件

[self addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];

}

return self;

}

(3)响应链:就是在一个控件不响应事件的时候,这个控件的父视图就响应,如果父视图响应不了就父视图的父视图去响应;

例:// 获取下一个响应者对象

UIResponder * responder = self.nextResponder;

// 判断是否存在responder,当没有下一个响应者对象时,responder不存在

while (responder) {

// isKindOfClass:判断responder是否是UIViewController或者UIViewController的子类

if ([responder isKindOfClass:[UIViewController class]]) {

// 获取响应者对象

ZAFirstViewController * vc = (ZAFirstViewController *)responder;

[vc.navigationController pushViewController:[[ZASecondViewController alloc] init] animated:YES];

}

// 当没有下一个响应者对象时,responder为空

responder = responder.nextResponder;

}

设置标签视图控制器:标签视图控制器(UITarBarController)可控制多个导航栏控制器(UINavigationController)和视图(UIView);

(1).设置窗口的根视图控制器:self.window=[[UIWindow alloc]init];—>[self.window makeKeyandVieble];—>创建一个(UITabBarController)UITabBarController *tabBarController=[[UITabBarController alloc]init];—>进行设置self.window.rootViewController=tabBarController;

(2)给UITabBarController设置按钮:先创建一个和UIViewController继承的类,并且导入到AppDelegat.h文件—>ViewController *vc=[[ViewController alloc]init];—>然后将导航控制器的根视图设为vc—>UINavigationController *navigation=[[UINavigationController alloc]initWithRootViewController:vc];—>并将UINavigationController以数组的形式传送到tabBarController(标签视图控制器)的viewControllers中来—>tabBarController.viewControllers=@[navigation,…];

(3)标签视图控制器上按钮的提示效果:navigation.tabBarItem=@“99+”;

(4)背影图片是否拉伸:tabBarController.tabBar.translucent=YES;

(5).给标签视图控制器设置背景图片:tabBarController.tabBar.backgroundImage=[UIImage imageName:@“图片名字”];

(6)给标签视图控制器设置系统的按钮的图片或文字:UITabBarItem *tabBarItem=[[UITabBarItem alloc]initWith…];——>给导航栏的tabBarItem赋值:navigationController.tabBarItem=barBarItem;

8月14日

自定义标签视图控制器:(1).创建一个继承UITabBarController的类并且将self.window.rootViewController设置为他:self.window=[[UIWindow alloc]init];

[self.window makeKeyAndVisible];

self.window.rootViewController=[[ZATabBarController alloc]init];

(2).在ZATabBArController类里去创建视图和导航栏控制器的根视图为视图并且赋予self.ViewControllers=@[导航栏控制器的名字,注:这是个数组];

(3)删除标签视图控制器里的控件:利用For 循环,然后就可以给其添加按钮或背景颜色

例:for(UIView *view in self.tabBar.subviews){

[view removeFromSuperView];

}

//给标签视图控制器设置背景

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kSercenWidth, 49)];

imageView.image=[UIImage imageNamed:@"mask_navbar"];

imageView.tag=11111;

imageView.userInteractionEnabled=YES;

[self.tabBar addSubview:imageView];

//设置标签控制器上的按钮

for (int i=0; i

if (i!=2){//当i=2时不进行创建按钮

UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(i*kSercenWidth/5, 0, kSercenWidth/5, 49)];

[self.tabBar addSubview:btn];

NSString *string=[NSString stringWithFormat:@"home_tab_icon_%d@2x",i+1];

[btn setBackgroundImage:[UIImage imageNamed:string] forState:UIControlStateNormal];

if (i>2) {

btn.tag=i+100-1;

}else{

btn.tag=i+100;

}

[btn addTarget:self action:@selector(changeNavigationController:) forControlEvents:UIControlEventTouchUpInside];

}

}

tabBarImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kSercenWidth/5, 49)];

tabBarImageView.image=[UIImage imageNamed:@"home_bottom_tab_arrow@2x"];

[imageView addSubview:tabBarImageView];

(4)设置标签选中那个子控制器:选中UINavigation—》self.seletedIndex=sender.tag-100;

例:-(void)changeNavigationController:(UIButton *)sender{

self.selectedIndex=sender.tag-100;

[UIView animateWithDuration:0.3 animations:^{

tabBarImageView.center=sender.center;

}];

}

8月15日

UIScrollView:滑动视图

(1).滑动视图的创建:UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(起始x,起始y,实际x,实际y)];

(2).设置滑动视图的内尺寸:scrollView.contentSize=CGSizeMake(里面的x的尺寸,里面y的尺寸);注:如果不想让其往左往右滑动请将其设置为0;

(3).取消水平或垂直滑轴:scrollView.showsHorizontalScrollIndicator=NO; scrollView.showVerticalScrollIndicator=NO;

(4).是否启动分页:scrollView.pagingEnabled=YES;

(5).是否给本页签订协议:scrollView.delegate=self;并在类名处签订

(6).给其设定最大放大值和最小缩小值:serollView.maximumZoomScale=2.5;scrollView.minimumZoomScalr=0.5;

(7).签订的方法有:

①.在其滑动将要结束的时候触发的方法:-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

//取出结束时的滑动距离

CGPoint point=*targetContentOffset;注:targetContentOffset是指针

//赋予页码控制器的选中页码

pageControl.currentPage=point.x/kScreenWidth;

}

②.在其滑动时实时追踪:- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

CGPoint point=scrollView;

pageControl.currentPage=point.x/kScreenWidth;

}

③.给其设定放大缩小的图片:- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

return [self.view viewWithTag:44];

}

(8).可以给scrollView添加手势:①.手势的创建:UITapGestureRecogmizer *tap=[[UITapGestureRecogmizer alloc]initWithtarget:self[给谁绑定] action:@selector(触发的方法)];

②.手势的点击触发数:tap.numberOFTapsRequired=2;当我点击2次时触发方法

③.给哪一部分添加手势:[scrollView addSubview:tap];

(9).跳转到指定的位置:[setContentOffset:CGPointMake(5*kScreen.width, 0)];

(10).取消系统的自动设定位置:系统自动判断页面上的容器,并且辅助将滑动视图及其子类内边距偏移—>self.automaticallyAdjustsScrollViewInsets=NO;

UIscrollView实例:

#import "ButtonViewController.h"

#define screenWidth [UIScreen mainScreen].bounds.size.width

#define screenHeight [UIScreen mainScreen].bounds.size.height

@implementation ButtonViewController{

UIScrollView *scrollView;

}

-(void)viewDidLoad{

[super viewDidLoad];

self.automaticallyAdjustsScrollViewInsets=NO;

self.title=@"自由缩放";

//scrollView的创建

scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];

//内尺寸

scrollView.contentSize=CGSizeMake(screenWidth, screenHeight);

//分页

scrollView.pagingEnabled=YES;

//取消滚轴

scrollView.showsHorizontalScrollIndicator=NO;

[self.view addSubview:scrollView];

//设置最大最小缩放值

scrollView.maximumZoomScale=2.5;

scrollView.minimumZoomScale=0.5;

//给自己添加协议

scrollView.delegate=self;

//手势

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecognizer:)];

//手势计数

tap.numberOfTapsRequired=2;

//添加到

[scrollView addGestureRecognizer: tap];

UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];

imageView.image=[UIImage imageNamed:self.string];

imageView.tag=44;

[scrollView addSubview:imageView];

}

//手势触发方法

-(void)tapGestureRecognizer:(UITapGestureRecognizer *)Tap{

//UIImageView *imageView=[self.view viewWithTag:44];

if (scrollView.zoomScale>1.0) {

[scrollView setZoomScale:1.0 animated:YES];

}else if (scrollView.zoomScale<1.0){

[scrollView setZoomScale:1.0 animated:YES];

}else{

[scrollView setZoomScale:2.5 animated:YES];

}

}

//当放大缩小是位于居中

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{

UIImageView *imageView=[self.view viewWithTag:44];

//CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;

//CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;

imageView.center = CGPointMake(screenWidth/2,screenHeight/2);

}

//给其指定放大缩小的图片

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

return [self.view viewWithTag:44];

}

@end

怎样使滑动视图实现无限循环:

多添加总图片的首尾各一张,将最后一张放在第一张前 ,将第一张放在最后一张以5,0,1,2,3,4,5,0的形式存在,将第一次显示的图片位于0,在添加视图时用[scrollView setContentOffset:CGPointMake(kScreen.width, 0)];达到目的,当其超过后面0的时直接跳转到1,当超过前面的5时直接跳转到4,例如下面

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

//给页码控制器赋值页码

NSInteger w=scrollView.contentOffset.x/kScreen.width-1;

if (w<0) {

w=4;

}else if(w>4){

w=0;

}

self.pageControl.currentPage=w;

//进行假的无限循环

if (scrollView.contentOffset.x<=0) {

//直接跳转到指定位置

[scrollView setContentOffset:CGPointMake(5*kScreen.width, 0)];

}else if(scrollView.contentOffset.x>=6*kScreen.width){

[scrollView setContentOffset:CGPointMake(kScreen.width, 0)];

}

}

UIPageControl:页码控制器

(1).页码控制器的创建:UIPageControl *pageControl=[[UIPageControll alloc]initWithFrame:CGRectMake(大小)];

(2).设定页码控制器的页码数:pageControl.numberOfPages=5;

(3).设定选中后的按钮颜色:pageControl.currenPageIndicatorTintColor=[UIColor redColor];

(4).给未选中的按钮设置颜色:pageControl.pageindicatorTintColor=[UIColor blueColor]

(5).赋予页码控制器的选中页码:pageControl.currentPage=point.x/kScreenWidth;

UITableView :表视图

(1).表视图的创建:UITableView *tableView=[[UITableView alloc]initWithFrame:尺寸大小 style:(包含有分组【UITableViewStyleGrouped】和无分组【UITableViewStylePlain】)];

(2).可通过获取plist文件来给UITableViewCell(框)赋值:在签订数据源和本身协议的情况下:tableView.dataSource = self;

①.进行获取路径:NSString * pPath = [[NSBundle mainBundle] pathForResource:@"provinces" ofType:@"plist"];

②.根据plist里的Root去用数组或字典接受:self.provincesArray = [NSArray arrayWithContentsOfFile:pPath];

self.citiesDic = [NSDictionary dictionaryWithContentsOfFile:cPath];

③.完成数据源协议必须要执行的方法:

【1】.设定有多少个组:- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

returnself.provincesArray.count;

}

【2】.设定每个组里的个数:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// 每一组有多少行

// 1. 获取每一个省份的名称

NSString * provinceName = self.provincesArray[section];

// 2. 通过省名,获取对应省地区数组

NSArray * citiesArr = self.citiesDic[provinceName];

// 3. 返回数组元素个数

return citiesArr.count;

}

【3】.赋给每个栏的字体:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//创建一个UITableViewCell的前提是在创建UITableView时给其注册一个字符串一样的 make-1,[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"make-1"];

注:这里采用了复用的方法

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"make-1" forIndexPath:indexPath];

// 1. 获取每一个省份的名称

NSString * provinceName = self.provincesArray[indexPath.section];

// 单元格没有一次性加载完成,而是在需要显示的时候,调用此方法,所以section每次不一定是不一样的

//NSLog(@"%@", provinceName);

// 2. 通过省名,获取对应省地区数组

NSArray * citiesArr = self.citiesDic[provinceName];

// 3. 获取每一行的下标取出相应的数组

NSString * cityName = citiesArr[indexPath.row];

cell.textLabel.text = cityName;

return cell;

}

【4】. 设置表视图顺序的索引

- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

return self.provincesArray;

}

【5】. UITableView的组头和组尾标题

// 这里的字符串是表视图加载的时候回全部加载,注意:数组越界

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

return self.provincesArray[section];

}

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

if (section == self.provincesArray.count - 1) {

return [NSString stringWithFormat:@"上有%@,下有riben", self.provincesArray[section]];

} else {

return [NSString stringWithFormat:@"上有%@,下有%@", self.provincesArray[section], self.provincesArray[section + 1]];

}

}

(3). 表视图索引条

tableView.sectionIndexBackgroundColor = [UIColor redColor];

tableView.sectionIndexColor = [UIColor blackColor];

tableView.sectionIndexTrackingBackgroundColor = [UIColor greenColor];

(4).//创建一个UITableViewCell的前提是在创建UITableView时给其注册一个字符串一样的 make-1,[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"make-1"];

注:这里采用了复用的方法

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"make-1" forIndexPath:indexPath];

在UITableView中如何使UITableViewCell点击后变为不选中状态:self.myTableView.allowsSelection = NO;

UITableViewController—>QQ组的收缩

#import "ZATableViewController.h"

#define CellD@"CellID"

@interface ZATableViewController ()

@property(nonatomic,strong)NSArray *AllArray;

@property(nonatomic,strong)NSMutableArray *stateArray;

@end

@implementation ZATableViewController

- (void)viewDidLoad {

[super viewDidLoad];

//获取文件

[self _newPlist];

//创建

[self _alloc];

}

-(void)_alloc{

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellD];

}

-(void)_newPlist{

NSString *path=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];

self.AllArray=[NSArray arrayWithContentsOfFile:path];

//创建出一个可以用于接收的可变数组

self.stateArray=[NSMutableArray array];

for(int i=0;i

[self.stateArray addObject:@"0"];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return self.AllArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//根据前面添加的可变数组判断是否输出多少行

if ([self.stateArray[section]isEqualToString:@"0"]) {

NSDictionary *dic=self.AllArray[section];

NSArray *arr=dic[@"friends"];

return arr.count;

}else{

return 0;

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellD forIndexPath:indexPath];

NSDictionary *dic=self.AllArray[indexPath.section];

NSArray *arr=dic[@"friends"];

cell.textLabel.text=arr[indexPath.row];

return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 70;

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

NSDictionary *dic=self.AllArray[section];

NSString *string=dic[@"group"];

UIButton *btn=[[UIButton alloc]init];

//给button赋tag值使其在触发方法时可以取到本该属于他的字符

btn.tag=section+10;

[btn setTitle:string forState:UIControlStateNormal];

[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

[btn setBackgroundImage:[UIImage imageNamed:@"tableCell_common"] forState:UIControlStateNormal];

//给组头添加方法

[btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

return btn;

}

-(void)buttonAction:(UIButton *)sender{

//先进行判断看可变数组中是否是0;如果是0就改为1;如果是1就改为0;

if ([self.stateArray[sender.tag-10]isEqualToString:@"1"]) {

//给可变数组里的固定位置换字符

[self.stateArray replaceObjectAtIndex:sender.tag-10 withObject:@"0"];

NSLog(@"%@",self.stateArray[sender.tag-10]);

}else{

[self.stateArray replaceObjectAtIndex:sender.tag-10 withObject:@"1"];

NSLog(@"%@",self.stateArray[sender.tag-10]);

}

//主要的代码:-------》刷新某一行或某一组

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-10] withRowAnimation:UITableViewRowAnimationAutomatic];

}

//一个section刷新

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];

[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//一个cell刷新

NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

UICollectionView: 集合视图

①.创建一个流水布局或自行创建一个继承与UICollectionViewLayout的layout:UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];

【1】.可设置单元格(layout.itemSize)的大小:layout.itemSize = CGSizeMake(44, [UIScreen mainScreen].bounds.size.height);

【2】.可设置单元格之间的间距和行的间距:间距:layout.minimumInteritemSpacing=1行:layout.minimumLineSpacing=1;

// 设置滑动方向

layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

②.创建一个集合视图并且设置位置:UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:(尺寸) collectionViewLayout:layout];

可以采用复用的方法来服用cell:[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];

// collectionView同样需要数据源方法来提供单元格

collectionView.dataSource = self;

collectionView.delegate = self;

和tableView差不多就是输出尺寸而不是距离,可自定义item

UIPickerView:可选中的滑动按钮视图

①.创建

②.签订协议UIPickViewDataSource和UIPickViewDelegat

③.复写方法:与tableView差不多{

、、并列多少个

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return 1;

}

一列有多少个

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

return self.dataArr.count;

}

、、返回一个的字符

// 返回每一个格子中的字符串

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

return self.dataArr[row];

}

}

④.0.从pickerView获取选中地区的名字

NSInteger selectedRow = [self.pickerView selectedRowInComponent:0];

8月31日多线程


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 148,827评论 1 317
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 63,511评论 1 266
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 99,318评论 0 218
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,108评论 0 189
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,112评论 1 266
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,387评论 1 185
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,905评论 2 283
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,657评论 0 177
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,144评论 0 223
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,744评论 2 225
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,100评论 1 236
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,565评论 2 222
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,041评论 3 216
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,769评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,301评论 0 178
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,173评论 2 239
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,313评论 2 242

推荐阅读更多精彩内容