1 UIWindow、UILabel、UIColor、UIScreen、UIViewController、UIView、UIControl、UIButton、IBOutlet、IBAction、UIStepper、   UISlider、    UISwitch、UITextField、UIAlertView、UIActionSheet、UINavigationController、UIBarButtonItem、UIImageView、UIScrollView、UIPageContro、UITableView、UITableViewController、UITableViewCell、UITabBarController、NSTimer、UICollectionViewController、UISegmentedControl
   2 =======================================================================
   3 在类内的init…方法中、get、set方法中,用_xxx实例变量。其他地方的访问都用self.属性
   4 内存管理修饰:
   5     copy:NSString,block
   6             copy:将对象的拷贝赋给引用,拷贝完的是不可变的。
   7             为什么用copy?
   8                 NSString类型的特点就是内容不可变的,但是给NSString赋值时,可以将NSMutableString赋过来,可能就会有其他持有MutableString的引用修改了这个字符串,结果你再用NSString访问字符串时就会发现,本来不可变的字符串在不知情的情况下被改变了。所以为了避免这种情况,就是用copy将内容复制成不可变之后再给NSString
   9 
  10             使用copy会更安全,但内存会被占用,一般情况下用strong不影响程序运行
  11 
  12     strong:除NSString以外的引用类型
  13         完全的拥有对象,引用计数器+1
  14     
  15     assign:基本数据类型、结构体、枚举
  16 ==========================================================================
  17 一、调试
  18 
  19 
  20 0.警告
  21     尽量一个警告都不要有
  22 
  23 1.错误
  24         1)红色提示
  25                     编译过不去的原因大部分是语法,检查括号的匹配,变量名称,作用域范围
  26 
  27         2)编译可以通过,可以运行
  28                 a。运行过程中程序崩溃
  29                         在debug区域的右侧,观察提示信息,信息的最上面会说明出错的类及方法的位置,然后找“reason”单词,看后面的提示
  30 
  31                 b。运行过程中没有问题,但是结果与期待不符
  32 
  33 2.问题的解决方法
  34             
  35     2.1    核心理念:掌控每一步中变量的变化
  36         
  37         使用debug工具,下断点,逐行跟踪程序的运行,查看每一行代码运行时,各个变量内存放的数据是否与期待存储的数据相一致,如果某一行代码执行后,发现变量中存储的值与期待不符,那么基本上原因就找到了,然后根据变量的整个赋值、运算过程再来分析为什么数据不符,解决了无数据或数据有误的情况后,程序基本就正常运行了。
  38 
  39     2.2 下断点位置的选择
  40             变量被改变之前的位置以及变量被改变之后的位置
  41 
  42     
  43 3工具的使用
  44         3.1 遇到断点停下来后,debug区域的三角按钮,点击后,程序继续自动 运行;如果后续运行中遇到了下一个断点,则停下来;如果后续运行中没有断点了,那么程序自动运行完才停下来
  45         3.2 step over:将一个方法整体作为执行的一步,一次性执行完,但,如果方法中有断点,则会进断点。进入断点后,可以通过点击三角号或step out都可以回到进入方法前的那个位置
  46         3.3 step into:可以进入方法的内部,继续逐行跟踪代码的执行过程
  47         3.4 step out:在方法内部中调试时,如果不想再继续跟踪,可以通过点击此按钮,快速回到进入该方法的那个位置
  48         3.5 调试过程中,如果想快速略过一段循环的话,可以在循环的后面添加断点,然后点击三角号,就会不再跟踪循环的过程,而快速执行到下一个断点的位置
  49 =============================================================================
  50 User Interface UIKit
  51 知识点
  52 一、UIWindow
  53 今天的目标:
  54 写出第一个iOS的程序,在界面上显示“Hello World”
  55 
  56 1.如何新建一个工程
  57     iOS—>Single View Application—>工程名、保存位置
  58 
  59 2.运行App
  60     点击三角符号或使用快捷键(Command + R)
  61     快捷键:Command+B 只是编译,不是运行
  62 
  63 3.工程的文件组成
  64 
  65 4.应用程序是如何运行起来的?
  66     1)main方法
  67                 int main(int argc, char * argv[])
  68     {
  69             @autoreleasepool {
  70                 return UIApplicationMain(argc, argv, nil,                 NSStringFromClass([AppDelegate class]));
  71             }
  72     }    
  73     UIApplicationMain()全局函数,一共做了三件事:
  74     a)根据第三个参数创建应用程序类的实例(nil默认UIApplication 系统默认)
  75     b)根据第四个参数创建应用程序代理类的实例
  76                               (Application Delegate代理     自己任意创建的类,遵守了协议才可以成为代理类)
  77     c)启动事件循环
  78     d)在代理类的didFinishLaunchingWithOptions:方法中编写启动程序后的界面创建及显示
  79 
  80 
  81 5.UIWindow
  82           1)是什么
  83               是界面显示的根本对象,要想出现显示的内容,那么一定是先创建window的实例
  84           2)作用是什么?
  85              是界面要显示的内容的父容器,在window的上面添加其他显示的内容
  86 
  87 6.frame
  88         1)是什么
  89               是CGRect类型的结构体
  90         2)描述了控件在父容器中的位置和大小
  91         CGRect{
  92             CGPoint  origin,
  93             CGSize     size
  94         }
  95         CGPoint{
  96             CGFloat  x,
  97             CGFloat y
  98         }
  99         CGSize{
 100             CGFloat width,
 101             CGFloat height
 102         }
 103   3)如何创建结构体的变量?
 104         使用全局函数  xxxMake();
 105         CGRect  —>  CGRectMake(x,y,width,height)
 106         CGPoint ->    CGPointMake(x,y);
 107         CGSize  ->    CGSizeMake(width,height); 
 108 
 109 例:
 110 AppDelegate.m
 111 #import "AppDelegate.h"
 112 
 113 @implementation AppDelegate
 114 
 115 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 116 {
 117     //应用程序启动后,第一个要执行动作的时机点,可以做第一个要出现的界面的设置
 118 
 119     //1.创建window的实例
 120     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];//读屏幕的大小    
 121    //2.设置window的背景色
 122     self.window.backgroundColor = [UIColor whiteColor];
 123     
 124     //3.创建并添加显示文本信息的标签
 125      UILabel *label = [[UILabel alloc]init];
 126     label.frame = CGRectMake(100, 150, 200, 40);
 127     label.text = @"Hello World";
 128     [self.window addSubview:label];//子试图添加到界面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
 129     
 130     //4.设置窗口可见(显示)
 131     [self.window makeKeyAndVisible];
 132     return YES;
 133 }
 134 =================================================================================================
 135 知识点
 136 二、应用程序设计理念:视图控制器(UIViewController)、视图(UIView)
 137       *利用视图控制器(底层)管理视图(外观),一对一
 138      1.视图的作用:负责显示的外观
 139      2.视图控制器的作用:创建界面、管理视图的生命周期
 140      3.UIViewController的UIView关系:系统的UIViewController中天生自带一个视图,UIView通过self.view属性来访问控制器自带的这个视图
 141      4.viewDidLoad方法:创建视图时会自动调用,而且只被调用一次,有关视图的初始化工作都会放在这个方法中
 142     5.使用VC的步骤: 
 143                step1:编写一个类,继承自UIViewController
 144                step2:重写类中的viewDidLoad方法,创建界面内容
 145                step3:在didFinishLaunching方法中,创建window的实例,创建vc的实例,将vc设置为window的rootViewController(根vc),显示window
 146            
 147 1.创建控制器
 148 AppDelegate.m
 149 #import "AppDelegate.h"
 150 #import "MyViewController.h"
 151 
 152 @implementation AppDelegate
 153 
 154 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 155 {
 156     self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
 157     self.window.backgroundColor=[UIColor greenColor];
 158     //1.创建控制器的实例  myVC自带一个视图
 159     MyViewController* myVC=[[MyViewController alloc]init];
 160     //2.将控制器设置为window的根视图控制器
 161     self.window.rootViewController=myVC;
 162     [self.window makeKeyAndVisible];
 163     return YES;
 164 }
 165 2.视图控制器viewDidLoad方法
 166      1).控制器拥有对其自带的视图的生命周期(创建--->销毁)管理权
 167   2).在视图的生命周期的第一个阶段就是执行viewDidLoad方法
 168   3).作用:用于创建视图,如:初始化界面、创建界面的显示元素
 169   4).特点:在整个生命周期中,只会被执行一次
 170 例:
 171 MyViewController.m
 172 (自己创建的类,继承了UIViewController父类方法,并重写了viewDidLoad方法)
 173 #import "MyViewController.h"
 174 
 175 @interface MyViewController ()
 176 @end
 177 
 178 @implementation MyViewController
 179 
 180 //控制器拥有对其自带的视图的生命周期(创建--->销毁)管理权
 181 //在视图的生命周期的第一个阶段就是执行viewDidLoad方法
 182 //作用:用于创建视图,如:初始化界面、创建界面的显示元素
 183 //特点:在整个生命周期中,只会被执行一次
 184 - (void)viewDidLoad
 185 {
 186     [super viewDidLoad];
 187     //设计view
 188     UILabel* label=[[UILabel alloc]init];
 189     label.frame=CGRectMake(110, 200, 100, 40);
 190     label.text=@"HelloWlord";
 191     //添加到控制器自带的那个视图里面
 192     [self.view addSubview:label];    
 193 }
 194 @end
 195 ==========================================================================================================================
 196 知识点
 197 三、视图(UIView)与控件(UIControl)
 198 1.UIView类
 199    1.什么是视图
 200       看得见的都是视图
 201    2.什么是控件 
 202      一种特殊的视图,都是UIControl的子类,不仅具有一定的显示外观,还能响应高级事件,与用户交互。严格意义上UILabel不是控件,因为label不能响应用户交互事件。
 203   3 术语的理解:
 204    视图:一个大一点的显示区域,里面可以容纳控件,做容器讲
 205    控件:容器中包含的子元素
 206 2.UILabel标签
 207  1. 是什么?
 208       静态文本内容的展示控件
 209  2.label属性
 210     1)text:显示文本的内容
 211     2)font:显示文本的字体
 212     3)numberOfLines:默认为1,显示的最大行数,0表示无上限
 213     4) lineBreakMode:换行模式, 省略头或尾
 214           NSLineBreakByTruncatingHead,    /* Truncate at head of line: "...wxyz" */
 215           NSLineBreakByTruncatingTail,    /* Truncate at tail of line: "abcd..." */
 216           NSLineBreakByTruncatingMiddle    /* Truncate middle of line:  "ab...yz"
 217     5)adjustsFontSizeToWidth:是否调整字体大小适应控件宽度  yes;
 218     6)  textColor:设置文本的颜色
 219     
 220 例:
 221 MyViewController.m
 222 - (void)viewDidLoad
 223 {
 224     [super viewDidLoad];
 225     //设计view
 226     UILabel* label=[[UILabel alloc]init];
 227     label.frame=CGRectMake(110, 200, 100, 60);
 228     //设置字体颜色
 229     label.textColor=[UIColor whiteColor];
 230     //设置最大显示行数
 231     label.numberOfLines=2;
 232     //设置标签内容的字体
 233     label.font=[UIFont systemFontOfSize:20];
 234     //设置换行模式
 235     label.lineBreakMode=NSLineBreakByTruncatingHead;
 236     //调整字体大小
 237     //label.adjustsFontSizeToFitWidth=YES;
 238     label.text=@"HelloWlord HelloWlord HelloWlord";
 239     //添加到控制器自带的那个视图里面
 240     [self.view addSubview:label];
 241     
 242 }
 243 3.UIButton按钮
 244    1.什么是按钮?
 245         可以与用户交互,能够点击的一种控件
 246    2.创建方式
 247          工厂方法创建,使用系统模式
 248    3.常用属性
 249        1)frame :按钮大小
 250        2)backgroundColor:按钮背景色
 251    3)setBackgroundImage:按钮背景图
 252              1.点击images.xcassets文件,将要添加的图片拖拉进文本框,左边框修改图片名字
 253              2.点击下方Show Slicing按钮
 254              3.在下方进行缩小放大操作
 255              4.点击图片中Start Slicing按钮进行裁剪,再点击中间按钮
 256              5.九切片:横3线:1线范围不变,1-2线之间复制,2-3线裁剪省掉,3线不变
 257              6.将照片名字添加到程序
 258       [button setBackgroundImage:[UIImage imageNamed:@"bg"]forState:UIControlStateNormal];
 259        4)tintColor:按钮字体颜色
 260        5)   setTitle:点击按钮的状态
 261       UIControlStateNormal       = 0, 正常按下
 262          UIControlStateHighlighted  = 1 << 0, 长按状态
 263          UIControlStateDisabled     = 1 << 1,  
 264          UIControlStateSelected     = 1 << 2,
 265  4.添加事件
 266 ***点一次按钮,执行调用一次方法
 267        addTarget:为按钮添加响应事件,即点击按钮时需实现的功能
 268  参数:             1.target:让当前控制器对象成为处理响应的对象
 269            2.action:处理事件的对象所使用的方法
 270            3.events:添加对按钮的什么事件的处理
 271          [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
 272  例:  
 273 MyViewController.m
 274 #import "MyViewController.h"
 275 
 276 @interface MyViewController ()
 277 @property(nonatomic,strong)UILabel *label;//设置全局变量
 278 
 279 @end
 280 
 281 @implementation MyViewController
 282 
 283 - (void)viewDidLoad
 284 {
 285     [super viewDidLoad];
 286     //使用工厂方法创建button对象
 287     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
 288     //设置frame属性  
 289     button.frame = CGRectMake(100, 200, 100, 40);
 290     //设置按钮上的文字
 291     [button setTitle:@"OK" forState:UIControlStateNormal];
 292     [button setTitle:@"KO" forState:UIControlStateHighlighted];
 293     //设置按钮的背景色
 294     //button.backgroundColor = [UIColor lightGrayColor];
 295     //设置按钮的背景图
 296     [button setBackgroundImage:[UIImage imageNamed:@"bg"] forState:UIControlStateNormal];
 297     //设置按钮的图片
 298     //[button setImage:[UIImage imageNamed:@"wifi"] forState:UIControlStateNormal];
 299     //为按钮添加响应事件
 300     //target:让当前控制器对象成为处理响应的对象
 301     //action:处理事件的对象所使用的方法
 302     //events:添加对按钮的什么事件的处理
 303     [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
 304     //添加按钮到视图中
 305     [self.view addSubview:button];
 306     
 307     //添加一个UILable
 308     UILabel *label = [[UILabel alloc]init];
 309     self.label = label;
 310     label.frame = CGRectMake(110, 50, 100, 40);
 311     label.text = @"Hello World";
 312     [self.view addSubview:label];
 313 }
 314 
 315 //处理事件的对象所使用的方法
 316 -(void)click
 317 {  
 318     self.label.text = @"Hello Kitty";
 319     //NSLog(@"click.....");
 320 }
 321 
 322 @end
 323 AppDelegate.h
 324 @implementation AppDelegate
 325 
 326 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 327 {
 328     self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
 329     self.window.backgroundColor=[UIColor whiteColor];
 330     MyViewController* vc=[[MyViewController alloc]init];
 331     self.window.rootViewController=vc;
 332     [self.window makeKeyAndVisible];
 333     
 334     return YES;
 335 }
 336 作业:
 337 2.做一个小的应用
 338     界面中有一个按钮,每次按下按钮,界面多一个UILabel
 339     要求:
 340     1)label之间间隔10个点的距离
 341     2)所有label和屏幕左边距离20个点
 342     3)所有label宽280,高20
 343     4)每个Label的内容进行叠加(Hello,HelloWorld,HelloWorldWorld,......)
 344 #import "MyViewController.h" 
 345 
 346 @interface MyViewController ()
 347 //@property(nonatomic,strong)UILabel* label;
 348 @property(nonatomic,assign)int y;
 349 @property(nonatomic,strong)NSMutableString* str;
 350 
 351 @end
 352 
 353 @implementation MyViewController
 354 
 355 - (void)viewDidLoad
 356 {
 357     [super viewDidLoad];
 358     UIButton* button=[UIButton buttonWithType:UIButtonTypeSystem];
 359     
 360     button.frame=CGRectMake(130, 40, 60, 20);
 361     button.backgroundColor=[UIColor yellowColor];
 362     button.tintColor=[UIColor redColor];
 363     [button setTitle:@"on" forState:UIControlStateNormal];
 364     [button setTitle:@"off" forState:UIControlStateHighlighted];
 365     
 366         //响应事件
 367         [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
 368         [self.view addSubview:button];
 369         self.y=100;//初始化
 370         self.str=[[NSMutableString alloc]initWithString:@"Hello"];//初始化
 371 }
 372 -(void)show{
 373     UILabel* label=[[UILabel alloc]init];
 374     label.backgroundColor=[UIColor greenColor];
 375     label.text=self.str;
 376     label.frame=CGRectMake(20, self.y, 280, 20);
 377     label.adjustsFontSizeToFitWidth=YES;
 378     label.textColor=[UIColor redColor];
 379     [self.view addSubview:label];
 380     //只改变当前使用的值,出了范围就是改变后的值
 381      self.y+=30;
 382     [self.str appendString:@"Wlord"];
 383     
 384 }
 385 
 386 @end
 387 =======================================================================================================================
 388 知识点
 389 四、interface Builder(简称 IB) 界面构建器
 390 
 391 1.interface Builder
 392 设置界面
 393    1.1 是什么?
 394      一个可视化的界面编辑工具软件,在xcode4之后整合到了xcode中
 395 
 396    1.2 作用?
 397      通过可视化的界面设置,能够少写或不写代码而完成界面的设计,从而减少在控制器的viewDidLoad中写的大量有关创建控件及设置属性的代码
 398 
 399   1.3 工作原理
 400     将界面所需控件的设置保存到xib文件中,在创建控制器实例的时候,系统会根据指定的xib文件来自动创建视图中的各个控件的实例、设置实例的属性,将其用于对控制器自带的视图的初始化中。所以,在创建控制器实例时,需要使用initWithNibName方法来指定有关视图的配置需要加载的文件是哪一个
 401 MyViewController* vc=[[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];//参数1:文件名  参数2:nil通常
 402 
 403   1.4 所需文件
 404              XxxxYyyy.h 
 405              XxxxYyyy.m
 406              XxxxYyyy.xib  (xml  interface builder)
 407 
 408 2. IBOutlet,IBAction
 409       由于控制器的视图中所需的控件都由系统根据xib文件自动创建完成,所以view已经对这些控件对象是strong强引用了,但是,此时控制器无法访问系统创建的这些控件,于是需要通过特殊的方法来获取系统创建的这些对象的引用。
 410  1.1使用方式
 411      1. 如果,是对IB中的对象添加属性访问,让控件成为一个外界可以访问的输出口,则通过连线的方式,使其成为控制其的IBOutlet属性
 412 @property (weak, nonatomic) IBOutlet UILabel *infolabel;//通过连线后生成的属性(Ctrl+鼠标左键)
 413 
 414     2.如果,是对IB中的对象添加事件的响应,则通过连线的方式,为控件添加IBAction行为事件
 415 
 416             添加连线的方式:
 417                 a。打开拆分视图,左边为xib文件,右边为xib对应的控制器m文件
 418                 b。选中控件,按下control
 419                 c。在控件上按下鼠标左键,拖拽到右侧的指定位置
 420                 d。如果是添加输出口,则拖拽到扩展中
 421                  @property (weak, nonatomic) IBOutlet UILabel *valueLabel;
 422                 e。如果是添加action,则拖拽到类的实现中,change方法
 423                  - (IBAction)changeValue:(UIStepper *)sender {
 424    
 425                   }
 426  1.2 action的参数
 427      在为控件添加事件响应时,可以添加一个参数,默认为id类型,代表的是发生事件的源头是哪个控件,或者说本次事件的发送者是谁
 428      在需要用到这个参数来区分连到同一个方法上的多个控件的时候,可以在连线时,选择type参数,设置为具体的某种控件类型,不需要再在方法内进行类型转换了。
 429 例:
 430 三个button控制一个label,并且将button的内容赋给label。只需将三个button都连线到一个方法当中。
 431 - (IBAction)click:(UIButton *)sender {
 432     self.infolabel.text=[sender titleForState:UIControlStateNormal];
 433 }
 434 
 435  1.3 使用IB连线时的注意事项
 436                 如果对IB中的控件连线后,又删除,那么此时删掉的只是代码中的变量或方法,并没有删掉界面中控件记录的连线,一定要在xib中,选中控件,右键,查看绑定的线有几条,将不需要的连线删掉,才不会出错。
 437 =================================================================================
 438 知识点
 439 五、基础控件(UIStepper   UISlider    UISwitch  UITextField )
 440 1.UIStepper 步进控件 
 441 精确掌握
 442      
 443             1.重要属性:
 444                     .value 初始值
 445                     .maximumValue 最大值
 446                     .minimumValue 最小值
 447                     .stepValue 间隔
 448             2.常用事件:
 449                     value Changed事件:当数值改变时触发
 450 
 451 2 .UISlider 滑块控件
 452 快速滑动的方式得到一个可变数值
 453             1.重要属性:
 454                     .value
 455             2.重要事件:
 456                     value Changed事件:当数值改变时触发
 457     //显示滑块控件的初始值
 458     self.sliderLabel.text = [NSString stringWithFormat:@"%.2lf",self.slider.value];
 459 例:三个滑块控件控制一个label的渐变色,红、绿、蓝
 460 #import "ColorViewController.h"
 461 
 462 @interface ColorViewController ()
 463 @property (weak, nonatomic) IBOutlet UISlider *redSlider;
 464 @property (weak, nonatomic) IBOutlet UISlider *greenSlider;//连接控件生成属性
 465 @property (weak, nonatomic) IBOutlet UISlider *blueSlider;
 466 
 467 @property (weak, nonatomic) IBOutlet UILabel *label;
 468 
 469 @end
 470 
 471 @implementation ColorViewController
 472 
 473 
 474 - (void)viewDidLoad
 475 {
 476     [super viewDidLoad];
 477     self.label.backgroundColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:1];
 478 }
 479 
 480 - (IBAction)changeRedColor:(UISlider *)sender {
 481     self.label.backgroundColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:1];//三个控件共用一个方法,赋给label
 482 }
 483 
 484 @end
 485 
 486 3. UISwitch 开关控件
 487             1.重要属性:
 488                     .on (BOOL)   获取或设置开关的状态
 489                     .enabled(BOOL) 获取或设置控件是否可用
 490                  //修改switch状态
 491                  [self.switchControl setOn:NO animated:YES];
 492                  //设置switch不可用
 493                   self.switchControl.enabled = NO;
 494             2.重要的事件:
 495                     valueChanged事件
 496 例:两个开关。主控制辅,主开,则辅可以开关。主关,则辅不可操作
 497 - (IBAction)mainSwitchChanged:(UISwitch *)sender {
 498     //根据当前sender的状态来决定下面的switch的状态
 499     [self.otherSwitch setOn:sender.on animated:YES];
 500     //下面的switch能不能用,取决于sender的状态
 501     //sender 如果为YES,enabled为YES
 502     //sender 如果为NO,enabled为NO
 503     self.otherSwitch.enabled = sender.on;
 504 }
 505 
 506 4.UITextField 文本框控件
 507         4.1 是什么?
 508                     是单行的文本输入框,支持用户的输入
 509 
 510         4.2 属性
 511                     .text  获取或设置文本框内的文本
 512                     … …
 513         
 514         4.3 系统弹出的键盘
 515         第一响应者:当用户触摸界面时,系统会根据手指触摸的位置层层定位到具体的控件,如果,本次触点在文本框控件的区域内,那么文本框就负责对本次的触碰事件进行响应,由于文本框比较特殊,所以系统自动将文本框设置为响应事件的第一关,并且自动弹出键盘。
 516 
 517                 插播:
 518                     当用户点击屏幕后,首先开启的是查找hit-View的过程。从window开始,给所有直接子视图发hit-Test的消息,直到某一个控件没有子视图了,并且这个触点在这个子视图中,则返回这个控件,于是hit-View找到了
 519                     找到hit-View后,view则成为了需要第一个为这个事件提供响应的对象,如果,该对象没有提供事件响应,则该事件对象会向视图的父视图继续传递,如果父视图依然没有提供响应,则继续向上传递,直到传递到UIApplication对象,依然没有处理的话,则抛弃该事件。这个过程叫做响应者链。
 520 
 521         4.4 如何关闭键盘
 522                     方法一:让键盘放弃第一响应者的身份即可
 523                         [self.textField resignFirstResponder];
 524 
 525                     方法二:让键盘所在的父视图结束编辑状态
 526                         [self.view endEditing:YES];
 527 
 528         4.5什么时候关闭键盘呢?(关闭键盘的时机)
 529                     时机一:点击键盘右下角的按键   选择Did End On Exit事件                            为文本框添加一个  事件
 530 
 531                     时机二:点击屏幕的空白部分
 532                               重写控制器的touchesBegan:withEvent:方法 (不用连接方法)
 533                         
 534         
 535 例:
 536 #import "MyViewController.h"
 537 
 538 @interface MyViewController ()
 539 @property (weak, nonatomic) IBOutlet UITextField *textField;
 540 
 541 @end
 542 
 543 @implementation MyViewController
 544 
 545 - (void)viewDidLoad
 546 {
 547     [super viewDidLoad];
 548     //self.textField.text = @"xxxxxxx";
 549     
 550 }
 551 
 552 - (IBAction)openKeyboard:(UIButton *)sender {
 553     //让文本框成为第一响应者
 554     [self.textField becomeFirstResponder];
 555     
 556 }
 557 - (IBAction)closeKeyboard:(UIButton *)sender {
 558     //方式一:让文本框放弃第一响应者的身份
 559     //[self.textField resignFirstResponder];
 560     //方式二:让文本框的父视图放弃编辑状态
 561     [self.view endEditing:YES];
 562 }
 563 
 564 //时机一:点击键盘右下角的按键,该事件触发 键盘收起
 565 - (IBAction)tapReturn:(UITextField *)sender {
 566     //[sender resignFirstResponder];
 567     [self.view endEditing:YES];
 568 }
 569 
 570 //时机二:点击空白屏幕 键盘收起
 571 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
 572     [self.view endEditing:YES];
 573 }
 574 
 575  
 576 作业:
 577 1.界面如下:
 578         [输入框][发送按钮]
 579         1)当点击发送按钮后,界面上会出现一个UILabel,内容就是输入框中的内容,此时要求,收起键盘,清空输入框
 580         2)当用户点击键盘右下角的按键时,功能和1相同
 581         3)label本身设置为宽280,高40,距离左边20个点
 582             4)多个label不能重合
 583 #import "MyViewController.h"
 584 
 585 @interface MyViewController ()
 586 //发送按钮
 587 @property (weak, nonatomic) IBOutlet UIButton *sendButton;
 588 //文本框
 589 @property (weak, nonatomic) IBOutlet UITextField *textField;
 590 
 591 @property(nonatomic,assign)float y;
 592 
 593 @end
 594 
 595 @implementation MyViewController
 596 
 597 - (void)viewDidLoad
 598 {
 599     [super viewDidLoad];
 600     //设置按钮属性
 601     self.sendButton.backgroundColor=[UIColor redColor];
 602     self.y=100;
 603     
 604     }
 605 //点击发送按钮,响应事件
 606 - (IBAction)openKeyBound:(UIButton *)sender {
 607     UILabel*label=[[UILabel alloc]init];
 608     label.frame=CGRectMake(20, self.y, 280, 40);
 609     NSLog(@"%@",self.textField.text);
 610     label.text=self.textField.text;
 611     [self.view addSubview:label];
 612     self.y+=60;
 613     [self.view endEditing:YES];
 614     self.textField.text=nil;
 615     
 616     }
 617 
 618 //点击左下角收起键盘
 619 - (IBAction)tapReturn:(UITextField *)sender {
 620     UILabel*label=[[UILabel alloc]init];
 621     label.frame=CGRectMake(20, self.y, 280, 40);
 622     NSLog(@"%@",self.textField.text);
 623     label.text=self.textField.text;
 624     [self.view addSubview:label];
 625     self.y+=60;
 626     [self.view endEditing:YES];
 627     self.textField.text=nil;
 628     [self.view endEditing:YES];
 629 }
 630 
 631         
 632 
 633 2.界面如下:
 634         [输入框 账号]
 635         [输入框 密码]
 636         [登录   按钮]
 637         [UILabel 显示当前状态(登录后显示用户名,没登录显示未登录)]
 638             1)用户输入完用户名和密码后,点击登录,判断是否可以登录,如果登录成功,label上显示当前用户的用户名,如果没有登录,显示未登录
 639             2)当用户没有填写用户名和密码时,点击登录,提供用户输入用户名和密码
 640             3)第一个textField支持Next功能,第二是Done
 641 #import "MyViewController.h"
 642 
 643 @interface MyViewController ()
 644 
 645 @property (weak, nonatomic) IBOutlet UITextField *textUse;//文本框1连线
 646 @property (weak, nonatomic) IBOutlet UITextField *textPassword;//文本框2连线
 647 
 648 @property (weak, nonatomic) IBOutlet UILabel *labelShow;//显示登陆成功的连线
 649 @property (weak, nonatomic) IBOutlet UILabel *label;//显示输入有误的连线
 650 
 651 
 652 @end
 653 
 654 @implementation MyViewController
 655 
 656 
 657 - (void)viewDidLoad
 658 {
 659     [super viewDidLoad];
 660     
 661 }
 662 
 663 - (IBAction)buttonValue:(UIButton *)sender {
 664     if ([self.textUse.text isEqualToString:@"fcp"]&&[self.textPassword.text isEqualToString:@"123"]) {
 665         self.labelShow.text=@"fcp用户登陆成功";
 666         self.label.text=@"";
 667     }else{
 668         self.label.text=@"用户名或密码错误";
 669         self.labelShow.text=@"未登录";
 670         
 671     }
 672     [self.view endEditing:YES];
 673         self.textUse.text=nil;
 674         self.textPassword.text=nil;
 675   
 676     
 677 }
 678 //实现next功能
 679 - (IBAction)frist:(UITextField *)sender { //文本框1连线,实现点回车到next功能
 680     [self.textPassword becomeFirstResponder];
 681 }
 682 - (IBAction)done:(UITextField *)sender {
 683     [self.view endEditing:YES];
 684 }
 685 
 686 @end
 687 
 688 =====================================================================
 689 知识点
 690 六、UIAlertView、UIActionSheet
 691 
 692 1.UIAlertView(警告框)
 693     1.1 创建警告框,设置样式
 694 - (IBAction)alertView:(UIButton *)sender {//创建button按钮
 695     //创建警告框的实例
 696     //UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"提示信息message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
 697 //如有多个otherButtonTitles,先显示otherButtonTitles。otherButtonTitles有一个,cancelButtonTitle在左边
 698     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"提示信息message" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];
 699     //显示警告框
 700     [alert show];//系统自带show方法
 701 }
 702     1.2创建有提示信息输入的警告框
 703 - (IBAction)alertViewInput:(id)sender {
 704     //创建警告框
 705     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"enter your login info" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
 706     //设置警告框的样式
 707     alert.alertViewStyle = UIAlertViewStylePlainTextInput;
 708     //显示警告框
 709     [alert show];
 710 }
 711      1.3创建有提示信息输入的警告框
 712     UIAlertViewStyleDefault
 713     UIAlertViewStyleSecureTextInput,//单行密码形式
 714     UIAlertViewStylePlainTextInput, //单行正常形式
 715     UIAlertViewStyleLoginAndPasswordInput//用户名和密码
 716       alert.alertViewStyle = UIAlertViewStylePlainTextInput;
 717 
 718      1.4 如何获取用户在警告框上的选择——委托
 719           a)什么是委托:
 720                     一个对象(对象A)让另一个对象(对象B)帮它做事情(发消息)。
 721           b)委托协议:
 722                     是委托方要求代理方所符合的规则,这些规则对应的就是一组方法,并且每个方法的第一个参数都是委托方的引用,每个方法的名称用于描述方法的执行时机
 723           c)设置控制器实例成为alertView的代理方的步骤
 724                     1)控制器遵守协议//委托方定义协议的名称要求"委托方name+Delegate"
 725                     2)控制器实现协议中的方法//要想成为代理方,必须满足委托方定义的协议
 726                                                   //第一个参数永远是委托方的引用,点进协议直接复制
 727                     3)在创建UIAlertView时设定控制器实例为代理方法
 728 
 729 例:通过输入用户名和密码信息,点击按钮。取出用户名和密码
 730 /*1.设置控制器成为警告框的代理,需要控制器(代理方)遵守协议*/
 731 @interface AlertViewController ()<UIAlertViewDelegate>
 732 @end
 733 @implementation AlertViewController
 734 
 735 - (IBAction)alertViewDelegate:(id)sender {
 736 //3.设置了警告框的代理方为当前控制器实例,于是,当用户点击了警告框上的某个按钮时该动作的处理就交给了控制器实例来响应
 737     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"your choice" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];  //self
 738     alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
 739     [alert show];
 740 }
 741 /*2.设置控制器成为警告框的代理,实现协议中的方法选择哪个方法实现,根据不同的方法对应的执行时机可以从方法名判断发消息的时机。
 742  方法的第一个参数一定是委托方的引用
 743  */
 744 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 745 {
 746     //目标:当点击YES按钮时,在控制器打印输入的用户名和密码
 747     if (alertView.cancelButtonIndex != buttonIndex) {
 748         //获取用户名
 749         NSString *loginName = [alertView textFieldAtIndex:0].text;
 750         //获取密码
 751         NSString *pwd = [alertView textFieldAtIndex:1].text;
 752         NSLog(@"name:%@ , pwd:%@",loginName,pwd);
 753     }
 754 
 755 结果:
 756 name:ggfg , pwd:sgg
 757 
 758         1.5 如何区分用户点击的按钮
 759             在  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法中,第二个参数为点击的按钮的索引,可以使用以下几种方法进行判断
 760                 方法一:直接判断索引值区分不同的按钮
 761                 方法二:根据索引值获取按钮的title,进行区分
 762                 方法三:判断索引是否是cancelButtonIndex进行区分
 763 //根据点击的按钮的索引获取点击的按钮的标题
 764     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
 765     方法1:判断标题来区分不同的动作
 766     if ([title isEqualToString:@"YES"]) {
 767     //    NSLog(@"点击了YES按钮");//根据点击了按钮OK执行的动作    }else{
 768     //    NSLog(@"点击了NO按钮");
 769     }
 770     方法3:也可以通过判断是不是cancel按钮的索引
 771     //来判断是否点击了cancel按钮
 772     if (alertView.cancelButtonIndex == buttonIndex)
 773     {
 774     //    NSLog(@"点击了NO按钮");
 775     }
 776 }
 777        
 778         
 779         1.6 如何获取alertView中输入框内的文本
 780                 利用alertView的textFieldAtIndex:方法,获得不同索引位置上的文本框,然后反问text属性即可
 781        NSString *pwd = [alertView textFieldAtIndex:1].text;
 782 
 783 
 784 2.UIActionSheet(操作表)
 785         2.1 创建操作表
 786     //创建actionSheet的实例
 787     UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"微博",@"微信",@"朋友圈", nil];
 788     //显示actionSheet
 789     [sheet showInView:self.view];
 790 
 791         2.2 判断用户点击的不同的按钮
 792                 a)需要控制器实例遵守协议
 793                 b)需要控制器实现协议中的方法
 794 #import "ActionSheetViewController.h"
 795 
 796 @interface ActionSheetViewController ()<UIActionSheetDelegate>//遵守协议
 797 
 798 @end
 799  - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
 800 {
 801     //区分点击的按钮
 802     //NSLog(@"%d",buttonIndex);//获取按钮标题对应的值
 803     //获取按钮上的标题
 804     //NSLog(@"%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
 805     if (actionSheet.cancelButtonIndex == buttonIndex) {
 806         //....点击了cancel
 807     }
 808     if (actionSheet.destructiveButtonIndex == buttonIndex) {
 809         //....点击了有破坏性的那个危险按钮
 810     }
 811 }
 812  813 =====================================================================
 814 知识点
 815 七、MVC     界面开发    
 816  1.什么是设计模式
 817      mvc只是其中一种,对某一类具体问题,总结出来的一套最优的解决方案
 818 1.MVC:
 819    1.Model(模型) View(视图)Controller(控制器) 的缩写
 820       Model:程序中处理数据逻辑  (数据存储、业务逻辑、多线程、网络传输、文件存储)
 821       View:程序中处理数据显示
 822       Controller:View和Model的媒介
 823     2.优点:
 824  1.耦合性低
 825  2.重用性高
 826  3.可维护性高   (结构清晰、可重用、方便维护)
 827 ***1.对引用数据类型,在保证在第一次访问改属性时,数组空间被创建出来。所以就得重写属性get和set方法:
 828 //重写suit属性的set方法
 829 -(void)setSuit:(NSString *)suit{
 830     if ([[Card allSuit]containsObject:suit]) {Card类,+(NSArray*)allSuit;
 831         _suit=suit;
 832         
 833     }
 834 }
 835 //重写cardInfo属性的get方法
 836 -(NSString*)cardInfo{
 837     _cardInfo=[self.suit stringByAppendingString:self.rank];
 838     return _cardInfo;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 839 }
 840 //保证在第一次访问改属性时,数组空间被创建出来
 841 -(NSMutableArray *)allCards{
 842     if (!_allCards) {
 843         _allCards=[NSMutableArray array];
 844     }
 845     return _allCards;
 846 }
 847 **2.随机数
 848 随机数从0开始的一个无符号正整数
 849    unsigned int index=arc4random()%52 0-51
 850         特点:不需要设置随机算子
 851        **3.lazy loading懒加载
 852 
 853 例:
 854 MVC案例(重在体会)
 855     里程碑1:
 856         能够随机出12张纸牌,将纸牌信息显示到界面上
 857 
 858     里程碑2:
 859         根据用户选中的纸牌,进行判断,生成新的结果,将结果显示到界面上
 860 
 861     里程碑3:
 862         再根据游戏规则统计分数    
 863 创建文件夹model:
 864 Card.h
 865 #import <Foundation/Foundation.h>
 866 /*
 867  用于描述一张纸牌
 868  属性:
 869     花色,大小,牌面信息,朝向,是否匹配
 870  方法:
 871  872  */
 873 @interface Card : NSObject
 874 
 875 @property(nonatomic,strong)NSString *suit;//花色
 876 @property(nonatomic,strong)NSString *rank;//大小
 877 @property(nonatomic,strong,readonly)NSString *cardInfo;
 878 
 879 @property(nonatomic,getter=isFaceUp)BOOL faceUp;
 880 @property(nonatomic,getter=isMatched)BOOL matched;
 881 
 882 -(instancetype)initWithSuit:(NSString *)suit andRank:(NSString *)rank;
 883 
 884 +(NSArray *)allSuit;
 885 +(NSArray *)allRank;
 886 
 887 @end
 888 
 889 Card.m
 890 
 891 #import "Card.h"
 892 
 893 @interface Card ()
 894 
 895 @property(nonatomic,strong,readwrite)NSString *cardInfo;
 896 
 897 @end
 898 
 899 @implementation Card
 900 
 901 //重写suit属性的set方法
 902 - (void)setSuit:(NSString *)suit{
 903     if ([[Card allSuit] containsObject:suit]) {
 904         _suit = suit;
 905     }
 906 }
 907 //重写rank属性的set方法
 908 - (void)setRank:(NSString *)rank{
 909     if ([[Card allRank] containsObject:rank]) {
 910         _rank = rank;
 911     }
 912 }
 913 
 914 //重写cardInfo属性的get方法
 915 - (NSString *)cardInfo{
 916     _cardInfo = [self.suit stringByAppendingString:self.rank];
 917     return _cardInfo;
 918 }
 919 
 920 - (instancetype)initWithSuit:(NSString *)suit andRank:(NSString *)rank{
 921     self = [super init];
 922     if (self) {
 923         self.suit = suit;
 924         self.rank = rank;
 925         self.faceUp = NO;
 926         self.matched = NO;
 927     }
 928     return self;
 929 }
 930 
 931 + (NSArray *)allSuit
 932 {
 933     return @[@"♠️",@"❤️",@"♣️",@"♦️"];
 934 }
 935 
 936 + (NSArray *)allRank
 937 {
 938     return @[@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];
 939 }
 940 
 941 @end
 942 
 943 Poker.h
 944 #import <Foundation/Foundation.h>
 945 
 946 /*用于描述一副扑克牌,能够容纳52张纸牌对象*/
 947 @interface Poker : NSObject
 948 
 949 @property(nonatomic,strong)NSMutableArray *allCards;
 950 
 951 @end
 952 Poker.m
 953 #import "Poker.h"
 954 #import "Card.h"
 955 
 956 @implementation Poker
 957 
 958 //重写allCards属性的get方法
 959 //保证在第一次访问该属性时,数组空间被创建出来
 960 - (NSMutableArray *)allCards{
 961     if (!_allCards) {
 962         _allCards  = [NSMutableArray array];
 963     }
 964     return _allCards;
 965 }
 966 
 967 -(id)init{
 968     self = [super init];
 969     if (self) {
 970         //创建52张纸牌对象,并放入到allCards中
 971         for (NSString *suit in [Card allSuit]) {
 972             for (NSString *rank in [Card allRank]) {
 973                 Card *card = [[Card alloc]initWithSuit:suit andRank:rank];
 974                 [self.allCards addObject:card];
 975             }
 976         }
 977     }
 978     return self;
 979 }
 980 
 981 @end
 982 Poker.h
 983 #import <Foundation/Foundation.h>
 984 #import "Poker.h"
 985 /*
 986   用于描述游戏类,存储游戏的数据,
 987   及对数据的处理逻辑
 988  */
 989 @interface Game : NSObject
 990 
 991 @property(nonatomic,strong)NSMutableArray *randomCards;
 992 @property(nonatomic)NSInteger score;
 993 
 994 -(instancetype)initWithCountCard:(NSInteger)count inPoker:(Poker *)poker;
 995 
 996 -(void)tapCardAtIndex:(NSInteger)index;
 997 
 998 @end
 999 
1000 Poker.m
1001 #import "Game.h"
1002 #import "Card.h"
1003 
1004 @implementation Game
1005 
1006 - (NSMutableArray *)randomCards{
1007     if (!_randomCards) {
1008         _randomCards = [NSMutableArray array];
1009     }
1010     return _randomCards;
1011 }
1012 
1013 -(instancetype)initWithCountCard:(NSInteger)count inPoker:(Poker *)poker{
1014     self = [super init];
1015     if (self) {
1016         //根据指定的count作为随机的次数
1017         //从poker中随机一张牌,将牌放入到randomCards
1018         for (NSInteger i=0; i<count; i++) {
1019             //使用随机函数得到一个0-51的下标
1020             unsigned int index = arc4random()%poker.allCards.count;
1021             //从poker中找到随机位置对应的牌
1022             Card *card = poker.allCards[index];
1023             //将牌从poker中移除
1024             [poker.allCards removeObject:card];
1025             //记录牌到randomCards中
1026             [self.randomCards addObject:card];
1027         }
1028         self.score=0;
1029     }
1030     return self;
1031 }
1032 
1033 /*
1034     1.获取index位置上的card对象
1035     2.如果card面儿朝上,则修改为面儿朝下
1036     3.如果card面儿朝下,首先修改为面儿朝上 
1037         将card与数组中其他的(已经朝上并且还没有被matched)牌进行比对
1038     4.比对的原则:
1039         如果两张牌的花色相同,则两张牌被matched
1040         如果两张牌的大小相同,则两张牌被matched
1041         否则,将被比对的牌翻回背面即可
1042  */
1043 - (void)tapCardAtIndex:(NSInteger)index{
1044     Card *card = self.randomCards[index];
1045     if (card.isFaceUp) {
1046         card.faceUp = NO;
1047     }else{
1048         card.faceUp = YES;
1049         for (NSInteger i=0;i<self.randomCards.count;i++) {
1050             if (i != index) {
1051                 Card *otherCard = self.randomCards[i];
1052                 if (otherCard.isFaceUp && !otherCard.isMatched) {
1053                     //比对花色
1054                     if ([card.suit isEqualToString:otherCard.suit]) {
1055                         card.matched = YES;
1056                         otherCard.matched = YES;
1057                         self.score+=1;
1058                     }else if([card.rank isEqualToString:otherCard.rank]){
1059                         card.matched = YES;
1060                         otherCard.matched = YES;
1061                         self.score+=4;
1062                     }else{
1063                         otherCard.faceUp = NO;
1064                     }
1065                 }
1066             }
1067         }
1068     }
1069 }
1070 
1071 @end
1072 GameViewController.h 继承ViewController的控制器
1073 GameViewController.m
1074 #import "GameViewController.h"
1075 #import "Game.h"
1076 #import "Poker.h"
1077 #import "Card.h"
1078 
1079 @interface GameViewController ()
1080 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
1081 //构成游戏而增加的属性
1082 @property(nonatomic,strong)Game *game;
1083 @property(nonatomic,strong)Poker *poker;
1084 @property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
1085 
1086 @end
1087 
1088 @implementation GameViewController
1089 
1090 //通过重写属性的get方法,实现lazy loading
1091 - (Poker *)poker{
1092     if (!_poker) {
1093         _poker = [[Poker alloc]init];
1094     }
1095     return _poker;
1096 }
1097 
1098 - (void)viewDidLoad
1099 {
1100     [super viewDidLoad];
1101     self.game = [[Game alloc]initWithCountCard:self.buttons.count inPoker:self.poker];
1102     [self updateCard];
1103     self.scoreLabel.text=@"0";
1104 }
1105 
1106 //更新Model中的牌信息到界面上
1107 -(void)updateCard{
1108     //遍历每一个按钮,根据按钮的坐标找到位置相同的随机的纸牌,并显示
1109     for (NSUInteger index = 0; index<self.buttons.count; index++) {
1110         //获取该位置的纸牌对象
1111         Card *card = self.game.randomCards[index];
1112         UIButton *button = self.buttons[index];
1113         [button setTitle:[self titleForCard:card] forState:UIControlStateNormal];
1114         [button setBackgroundImage:[UIImage imageNamed:[self imageNameForCard:card]]forState:UIControlStateNormal];
1115         button.enabled = !card.isMatched;
1116     }
1117     //更新分数标签
1118     self.scoreLabel.text=[NSString stringWithFormat:@"%d",self.game.score];
1119 }
1120 //根据纸牌信息返回按钮上要显示的文字
1121 -(NSString *)titleForCard:(Card *)card{
1122     return card.isFaceUp?card.cardInfo:@"";
1123 }
1124 
1125 //根据纸牌信息返回按钮上要显示的图片
1126 -(NSString *)imageNameForCard:(Card *)card{
1127     return card.isFaceUp?@"cardfront.png":@"cardback.png";
1128 }
1129 
1130 
1131 - (IBAction)chooseCard:(UIButton *)sender
1132 {
1133     NSInteger index = [self.buttons indexOfObject:sender];
1134     //将索引值传递给Model
1135     [self.game tapCardAtIndex:index];
1136    
1137     //更新界面
1138     [self updateCard];
1139 }
1140 
1141 @end
1142 AppDelegate.h
1143 AppDelegate.m
1144 #import "AppDelegate.h"
1145 #import "GameViewController.h"
1146 
1147 @implementation AppDelegate
1148 
1149 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1150 {
1151     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
1152     GameViewController *vc = [[GameViewController alloc]initWithNibName:@"GameViewController" bundle:nil];
1153     self.window.rootViewController = vc;
1154     [self.window makeKeyAndVisible];
1155     return YES;
1156 }
1157 1158 ================================================================================================================
1159  知识点
1160 八、多MVC开发    ( 多界面开发)
1161 1.多MVC
1162       每一个界面的显示都是一套独立的MVC,由于应用程序需要多个界面,所以构成了多套MVC。
1163       注意:其中C和V是绑在一起的,但是M由于实现了程序中的数据存储以及业务逻辑,是与C和V分开的一套体系,所以多套C+V组合,可以共用一个M。但是,不允许v的共用,每一个c都有自己的v,不能因为要切换界面,而让某一个c抛弃自己带的v,换别的c下面的v。
1164         
1165         1.1 多界面的切换
1166                 原理:更换了c就实现了更换了v
1167 
1168         1.2 如何实现c的更换?
1169                 方向:从controlA  —> 推出controlB
1170                             [controlA  presentViewController:]
1171 
1172                 方向:A推出B之后,想从B再回到A
1173                             [controlB  dismissViewController:];
1174 
1175 例:
1176 创建两个控制器类A和B,每个类的xib界面拉入一个button按钮,然后添加方法。
1177 功能:点击A界面的按钮回到B界面。点击B界面按钮返回到A 界面
1178 FristViewController.h
1179 FristViewController.m
1180 #import "FristViewController.h"
1181 #import "SecondViewController.h"
1182 
1183 @interface FristViewController ()
1184 
1185 @end
1186 
1187 @implementation FristViewController
1188 
1189 
1190 - (void)viewDidLoad
1191 {
1192     [super viewDidLoad];
1193     
1194 }
1195 - (IBAction)gotoSecondView:(id)sender {//A界面的方法
1196     //创建要推出的vc的实例
1197     SecondViewController* secondVc=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
1198     
1199     //从当前控制器下方推出新的vc
1200     [self presentViewController:secondVc animated:YES completion:nil];
1201 }
1202 SecondViewController.h
1203 SecondViewController.m
1204 #import "SecondViewController.h"
1205 
1206 @interface SecondViewController ()
1207 
1208 @end
1209 
1210 @implementation SecondViewController
1211 
1212 
1213 - (void)viewDidLoad
1214 {
1215     [super viewDidLoad];
1216     
1217 }
1218 //返回到第一个界面
1219 - (IBAction)goBack:(id)sender {//B界面的方法
1220     [self dismissViewControllerAnimated:YES completion:nil];
1221 }
1222 
1223 @end
1224 2.界面之间的正向传值
1225             2.1 什么是正向传值:                
1226 当从控制器A推出新的控制器B的时候,A将界面中接受的数据同时也传给了B,由B进行展示或处理的过程
1227 
1228         2.2 如何实现正向传值:
1229                 step1:为控制器B增加公开的属性用于接收外界传入的值
1230                 step2:控制器A为了推出B,会创建B的实例,创建完实例后,在推出之前,将要传递的属于存到B公开的属性中即可
1231                 step3:推出的控制器B在viewWillAppear中将数据展示到界面中
1232 
1233 例:
1234 控制器A界面的内容可以传到B界面显示
1235 FristViewController.h
1236 FristViewController.m
1237 #import "FristViewController.h"
1238 #import "SecondViewController.h"
1239 
1240 @interface FristViewController ()
1241 @property (weak, nonatomic) IBOutlet UILabel *textField;//界面1中的文字信息属性  连线A中的
1242 @end
1243 
1244 @implementation FristViewController
1245 
1246 
1247 - (void)viewDidLoad
1248 {
1249     [super viewDidLoad];
1250     
1251 }
1252 - (IBAction)gotoSecondView:(id)sender {//A界面的方法
1253     //创建要推出的vc的实例
1254     SecondViewController* secondVc=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
1255 
1256     //将文本框的值赋到的secondVc公开属性中
1257     secondVc.content=self.textField.text;
1258 
1259     //从当前控制器下方推出新的vc
1260     [self presentViewController:secondVc animated:YES completion:nil];
1261 }
1262 SecondViewController.h
1263 #import <UIKit/UIKit.h>
1264 
1265 @interface SecondViewController : UIViewController
1266 @property(nonatomic,strong)NSString*content;
1267 //公开属性
1268 @end
1269 
1270 SecondViewController.m
1271 #import "SecondViewController.h"
1272 
1273 @interface SecondViewController ()
1274 @property (weak, nonatomic) IBOutlet UILabel *label;//界面2的文本属性     连线B界面中的label
1275 @end
1276 
1277 @implementation SecondViewController
1278 
1279 
1280 //创建视图时被执行一次
1281 - (void)viewDidLoad
1282 {
1283     [super viewDidLoad];
1284     self.label.text=self.content;
1285 }
1286 //视图显示之前被执行,可以执行多次
1287 -(void)viewWillAppear:(BOOL)animated{
1288     [super viewWillAppear:animated];
1289     //显示公开的属性
1290      self.label.text=self.content;
1291 }
1292 //返回到第一个界面
1293 - (IBAction)goBack:(id)sender {//B界面的方法
1294     [self dismissViewControllerAnimated:YES completion:nil];
1295 }
1296 
1297 @end
1298 3.界面之间的反向传值
1299             3.1 什么是反向传值 ?
1300             从A推出了B之后,当从B返回到(dismiss)推出它的A时,传递了数据回来,由A进行显示的过程
1301 
1302         3.2 如何实现
1303                 step1:B中公开一个可以接收A引用的属性aVC
1304                 step2:A中公开一个可以接收返回数据的属性message
1305                 step3:A推出B之前,将自己的引用传给B
1306                 step4:B在dismiss之前,将要返回的数据传给持有的A的引用中公开的message属性
1307                 step5:在A的viewWillAppear中,显示message的内容
1308 
1309 例:
1310 B界面的内容返回到A界面
1311 
1312 1.方法一:缺点耦合度太高
1313 AViewController.h
1314 #import <UIKit/UIKit.h>
1315 
1316 @interface AViewController : UIViewController
1317 
1318 @property(nonatomic,strong)NSString *message;公开属性
1319 
1320 @end
1321 
1322 AViewController.m
1323 #import "AViewController.h"
1324 #import "BViewController.h"
1325 @interface AViewController ()
1326 @property (weak, nonatomic) IBOutlet UILabel *label; 连线label,显示返回的值
1327 
1328 @end
1329 
1330 @implementation AViewController
1331 
1332 
1333 - (void)viewDidLoad
1334 {
1335     [super viewDidLoad];
1336    
1337 }
1338 - (void)viewWillAppear:(BOOL)animated{
1339     [super viewWillAppear:animated];
1340     //呈现界面之前将message内的数据显示到标签上
1341     self.label.text = self.message;
1342 }
1343 
1344 - (IBAction)gotoBViewController:(id)sender {
1345     BViewController *bVC = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
1346     //将当前控制器(A)引用传给B
1347     bVC.aVC = self;
1348     [self presentViewController:bVC animated:YES completion:nil];
1349 }
1350 @end
1351 
1352 BViewController.h
1353 #import <UIKit/UIKit.h>
1354 #import "AViewController.h"
1355 
1356 @interface BViewController : UIViewController
1357 
1358 //公开一个属性,存放A的引用
1359 @property(nonatomic,strong)AViewController *aVC;
1360 @end
1361 BViewController.m
1362 #import "BViewController.h"
1363 
1364 @interface BViewController ()
1365 @property (weak, nonatomic) IBOutlet UITextField *textField; 连线文本框,B界面输入的内容
1366 
1367 @end
1368 
1369 @implementation BViewController
1370 - (void)viewDidLoad
1371 {
1372     [super viewDidLoad];
1373 }
1374 
1375 - (IBAction)goback:(id)sender {
1376     self.aVC.message = self.textField.text;
1377     [self dismissViewControllerAnimated:YES completion:nil];
1378 }
1379 
1380 @end
1381 2.方法二:
1382 4.使用 委托 实现 反向传值
1383             委托方:推出的B            代理方:推出B的那个A
1384 
1385             委托方要做的三件事:
1386         .h文件    a。定义协议1)协议名称 : 类名+Delegate  2)方法的第一个参数一定是委托方自己      3)方法名尽量体现发消息的时机
1387         .h文件    b。添加delegate属性 @property(nonatomic,weak)id<BViewControllerDelegate>  delegate;
1388         .m文件c。选择合适的时机给代理发消息
1389 
1390 
1391             代理方要做的三件事:
1392                 a。遵守协议
1393                 b。实现方法
1394                 c。将自己设置为代理方
1395 AViewController.h
1396 AViewController.m
1397 #import "AViewController.h"
1398 #import "BViewController.h"
1399 //代理方要做的三件事
1400 //1.遵守协议
1401 @interface AViewController ()<BViewControllerDelegate>
1402 
1403 @property (weak, nonatomic) IBOutlet UILabel *label;
1404 
1405 @end
1406 
1407 @implementation AViewController
1408 
1409 
1410 - (void)viewDidLoad
1411 {
1412     [super viewDidLoad];
1413    
1414 }
1415 - (IBAction)gotoB:(id)sender {
1416     BViewController *bVC = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
1417     //3.将自己设置为bVC的代理方
1418     bVC.delegate = self;
1419     [self presentViewController:bVC animated:YES completion:nil];
1420 }
1421 
1422 
1423 //2.实现方法
1424 - (void)bViewController:(BViewController *)bVC inputFinishedWithMessage:(NSString *)message{
1425     self.label.text = message;
1426 }
1427 BViewController.h
1428 #import <UIKit/UIKit.h>
1429 @class BViewController;
1430 
1431 //1.定义协议
1432 /*
1433  要点:  1)协议名称 : 类名+Delegate
1434         2)方法的第一个参数一定是委托方自己
1435         3)方法名尽量体现发消息的时机
1436  */
1437 @protocol BViewControllerDelegate <NSObject>
1438 -(void)bViewController:(BViewController *)bVC inputFinishedWithMessage:(NSString *)message;
1439 @end
1440 
1441 @interface BViewController : UIViewController
1442 //2.添加一个公开的delegate属性
1443 @property(nonatomic,weak)id<BViewControllerDelegate>  delegate;
1444 
1445 @end
1446 
1447 BViewController.m
1448 #import "BViewController.h"
1449 
1450 @interface BViewController ()
1451 @property (weak, nonatomic) IBOutlet UITextField *textField;
1452 
1453 @end
1454 
1455 @implementation BViewController
1456 
1457 
1458 
1459 - (void)viewDidLoad
1460 {
1461     [super viewDidLoad];
1462    
1463 }
1464 - (IBAction)goBack:(id)sender {
1465     //3.合适的时机,给代理方发消息
1466     [self.delegate bViewController:self inputFinishedWithMessage:self.textField.text];
1467     [self dismissViewControllerAnimated:YES completion:nil];
1468 }
1469 
1470 @end
1471 
1472 作业:
1473 
1474 1.作业1参考图片   homework1.png
1475 
1476 2.星座运程App
1477         a。界面1:有一个按钮“请选择您的星座”,用户点击后,跳转到第二个界面
1478         b。界面2:有12个星座(可以做成12个按钮)可选,选定后,返回到第一个界面
1479         c。界面1:显示选择的那个星座的运程
1480 AViewController.h
1481 AViewController.m
1482 #import "AViewController.h"
1483 #import "StarViewController.h"
1484 
1485 @interface AViewController ()<StarViewControllerDelegate>
1486 @property (weak, nonatomic) IBOutlet UILabel *resultLabel;连线返回值label的属性
1487 
1488 @end
1489 
1490 @implementation AViewController
1491 
1492 
1493 - (void)starViewController:(StarViewController *)sVC chooseStarWithMessage:(NSString *)mes{
1494     self.resultLabel.text = mes;
1495 }
1496 
1497 - (void)viewDidLoad
1498 {
1499     [super viewDidLoad];
1500 
1501 }
1502 
1503 - (IBAction)chooseStar:(id)sender {连线按钮的点击方法
1504     StarViewController *svc = [[StarViewController alloc]initWithNibName:@"StarViewController" bundle:nil];
1505     svc.delegate = self;
1506     [self presentViewController:svc animated:YES completion:nil];
1507 }
1508 
1509 
1510 @end
1511 
1512 StarViewController.h
1513 #import <UIKit/UIKit.h>
1514 @class StarViewController;
1515 
1516 @protocol StarViewControllerDelegate <NSObject>
1517 
1518 -(void)starViewController:(StarViewController *)sVC chooseStarWithMessage:(NSString *)mes;
1519 
1520 @end
1521 
1522 
1523 
1524 @interface StarViewController : UIViewController
1525 @property(nonatomic,weak)id<StarViewControllerDelegate> delegate;
1526 
1527 @end
1528 
1529 StarViewController.m
1530 #import "StarViewController.h"
1531 
1532 @interface StarViewController ()
1533 @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons; 连线建立4个星座的属性
1534 @property(nonatomic,strong)NSArray *array;
1535 
1536 @end
1537 
1538 @implementation StarViewController
1539 
1540 //重写array属性的get方法
1541 - (NSArray *)array{
1542     if (!_array) {
1543         _array = @[@"恭喜发财",@"万事如意",@"顺风顺水",@"新年快乐"];
1544     }
1545     return _array;
1546 }
1547 
1548 
1549 
1550 
1551 - (IBAction)chooseStar:(UIButton *)sender { 连线建立4个星座的方法
1552     
1553     NSInteger index = [self.buttons indexOfObject:sender];
1554     
1555     [self.delegate starViewController:self chooseStarWithMessage:self.array[index]];
1556     [self dismissViewControllerAnimated:YES completion:nil];
1557 }
1558 
1559 3.文本创建器
1560         a。界面1:有一个按钮“创建文本”,点击后进入到界面2
1561         b。界面2:
1562                     x:[TextField]   width:[TextField]
1563                     y:[TextField]   height:[TextField]
1564                     text:[TextField]
1565                     【确定按钮】
1566                     当用户点击确认按钮后,返回到界面1
1567         c。界面1:根据用户刚才的输入,创建一个UILabel对象,label的frame根据刚才的输入确定,label的内容也是根据输入确定,将label加入到界面1中
1568 FirstViewController.h
1569 FirstViewController.m
1570 
1571 #import "FirstViewController.h"
1572 #import "SecondViewController.h"
1573 @interface FirstViewController ()<SecondViewControllerDelegate>
1574 @property(nonatomic,assign)NSInteger x;
1575 @property(nonatomic,assign)NSInteger y;
1576 @property(nonatomic,assign)NSInteger w;
1577 @property(nonatomic,assign)NSInteger h;
1578 
1579 @end
1580 
1581 @implementation FirstViewController
1582 -(void)secondViewController:(SecondViewController *)secondVC sendX:(NSString *)x sendY:(NSString *)y sendWidth:(NSString *)width sendHeight:(NSString *)height sendTextField:(NSString *)textField{
1583     self.x=[x intValue] ;
1584     self.y=[y intValue] ;
1585     self.w=[width intValue] ;
1586     self.h=[height intValue] ;
1587     UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(self.x, self.y, self.w, self.h)];
1588     label.text=textField;
1589     [self.view addSubview:label];
1590 }
1591 
1592 
1593 - (void)viewDidLoad
1594 {
1595     [super viewDidLoad];
1596 }
1597 
1598 
1599 - (IBAction)gotoSecond:(UIButton *)sender { 连线创建文本按钮
1600     SecondViewController* second = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
1601     second.delegate = self;
1602     [self presentViewController:second animated:YES completion:nil];
1603 }
1604 
1605 @end
1606 SecondViewController.h
1607 c#import <UIKit/UIKit.h>
1608 @class SecondViewController;
1609 @protocol SecondViewControllerDelegate <NSObject>
1610 
1611 -(void)secondViewController:(SecondViewController*)secondVC sendX:(NSString*)x sendY:(NSString*)y sendWidth:(NSString*)width sendHeight:(NSString*)height sendTextField:(NSString*)textField;
1612 
1613 @end
1614 
1615 @interface SecondViewController : UIViewController
1616 @property(nonatomic,weak)id<SecondViewControllerDelegate> delegate;
1617 @end
1618 SecondViewController.m
1619 #import "SecondViewController.h"
1620 
1621 @interface SecondViewController ()
1622 @property (weak, nonatomic) IBOutlet UITextField *x;
1623 @property (weak, nonatomic) IBOutlet UITextField *width;
1624 @property (weak, nonatomic) IBOutlet UITextField *y;
1625 @property (weak, nonatomic) IBOutlet UITextField *height;
1626 @property (weak, nonatomic) IBOutlet UITextField *textField; 连线5个文本框
1627 
1628 @end
1629 
1630 @implementation SecondViewController
1631 
1632 - (void)viewDidLoad
1633 {
1634     [super viewDidLoad];
1635 
1636 }
1637 - (IBAction)goBack:(UIButton *)sender { 连线确定按钮
1638     
1639     [self.delegate secondViewController:self sendX:self.x.text sendY:self.y.text sendWidth:self.width.text sendHeight:self.height.text sendTextField:self.textField.text];
1640     [self dismissViewControllerAnimated:YES completion:nil];
1641     
1642 }
1643 
1644 
1645 @end
1646 
1647 =========================================================================
1648 知识点
1649 九、UINavigationController(导航控制器)
1650 
1651 1.UINavigationControlle
1652             1.1 是什么?
1653             继承自UIViewController,依然是一种控制器,但是,这种控制器没有具体的view,是管理控制器的控制器
1654 
1655         1.2 优点?
1656                 能够管理和控制VC的走向,比present方式更清晰
1657 
1658         1.3 如何使用?
1659                 step1:创建UINavigationController的实例
1660                 step2:创建一个具体的vc实例,并将这个vc设置为UINavigationController的根视图控制器
1661                 step3:设置navigationController为window的根视图控制器
1662                 step4:想推出新的vc时,可以使用pushViewController的方法
1663                 step5:想回退到上一个vc时,可以不写代码;或者是使用popViewController的方法
1664 
1665         1.4 内部原理
1666                 1)navi内部有一个可以存储多个vc的数组,就是self.navigationController.viewControllers属性;并且这个数组使用“栈”的方式来管理数据。“栈”的特点:先进后出,后进先出。
1667                 2)navi必须 有一个根视图控制器作为第一个展示的vc
1668                 3)push出一个新的vc时,就是往栈属性中入栈一个vc对象,新的vc入栈之前显示的那个vc不会被释放
1669                 4)pop现有vc时,才是将这个vc释放掉
1670                 5)不能pop根vc
1671 
1672 例:
1673 A界面退出B,B返回A
1674 AppDelegate.h
1675 AppDelegate.m
1676 #import "AppDelegate.h"
1677 #import "AViewController.h"
1678 
1679 @implementation AppDelegate
1680 
1681 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1682 {
1683     self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
1684     AViewController* aVC=[[AViewController alloc]initWithNibName:@"AViewController" bundle:nil];
1685     //创建导航控制器的实例
1686     UINavigationController*navi=[[UINavigationController alloc]initWithRootViewController:aVC];
1687     //将navi设置为window的跟视图
1688     self.window.rootViewController=navi;
1689     [self.window makeKeyAndVisible];
1690     return YES;
1691 }
1692 AViewController.h
1693 AViewController.m
1694 #import "AViewController.h"
1695 #import "BViewController.h"
1696 
1697 @interface AViewController ()
1698 
1699 @end
1700 
1701 @implementation AViewController
1702 
1703 
1704 - (void)viewDidLoad
1705 {
1706     [super viewDidLoad];
1707    
1708 }
1709 
1710 - (IBAction)gotoB:(id)sender {
1711     BViewController* bVC=[[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
1712     [self.navigationController pushViewController:bVC animated:YES];//通过navigationController属性退出B界面
1713 
1714 }
1715 
1716 @end
1717 BViewController.h
1718 BViewController.m
1719 #import "BViewController.h"
1720 
1721 @interface BViewController ()
1722 
1723 @end
1724 
1725 @implementation BViewController
1726 
1727 - (void)viewDidLoad
1728 {
1729     [super viewDidLoad];
1730     
1731 }
1732 //返回(也可以不写,通过navigationController自带的back返回)
1733 - (IBAction)goback:(id)sender {
1734     [self.navigationController popViewControllerAnimated:YES];
1735 }
1736 @end
1737 2.配置导航栏 
1738    只针对当前的界面
1739    1.访问导航栏:self.navigationItem
1740    2.导航栏包含三部分:
1741 
1742          1)左侧的按钮区域
1743         self.navigationItem.leftBarButtonItem/s
1744               self.navigationItem.leftBarButtonItem=r;方法同右侧
1745          2)中间的title
1746           self.title =  @“”;
1747       中间title部分:在viewDidLoad中配置导航栏标题,在哪个界面显示就配置的哪个界面的标题栏
1748 - (void)viewDidLoad
1749 {
1750     [super viewDidLoad];
1751     //配置导航栏标题
1752     self.title=@"A界面";
1753    
1754 }
1755     3)右侧的按钮区域
1756            self.navigationItem.rightBarButtonItem/s
1757 - (void)viewDidLoad
1758 {
1759     [super viewDidLoad];
1760     //配置导航栏标题
1761     self.title=@"A界面";
1762     //配置导航栏右侧按钮内容
1763     UIBarButtonItem* r=[[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStyleDone target:self action:@selector(gotoB:)];
1764     //self.navigationItem.rightBarButtonItem=r;
1765     UIBarButtonItem* r2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];//系统自带显示图标 搜索
1766     self.navigationItem.rightBarButtonItems=@[r,r2];
1767         4)按钮的类型:UIBarButtonItem类型
1768         可以使用initWithTitle。。方法创建自定义
1769         文本内容的按钮
1770         可以使用initWithBarButtonSystemItem…方法创建系统定义好的,有固定外观的按钮
1771 
1772 3.配置工具栏
1773      默认是全局的
1774      1. 访问工具栏:self.toolBarItem
1775      2.工具栏中加载的也是UIBarButtonItem类型的按钮
1776      3.将默认隐藏的工具栏显示出来,默认是隐藏
1777     self.navigationController.toolbarHidden = NO;
1778       1)只在一个界面显示,其他界面隐藏    ( 需要在其他界面加上self.navigationController.toolbarHidden = YES;)
1779 //和显示有关,和创建没有关系
1780 -(void)viewWillAppear:(BOOL)animated{
1781     //将默认隐藏的工具栏显示出来
1782     self.navigationController.toolbarHidden = NO;
1783 }
1784       2)设置推出bvc时,底部区域所有bar隐藏 (其他界面不需要修改)
1785              在需要设置导航的界面,通过viewDidLoad方法显示出来,然后在创建推出其它界面时隐藏bvc.hidesBottomBarWhenPushed=YES;
1786 
1787 - (IBAction)gotoBViewController:(id)sender {
1788     BViewController *bvc = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
1789     //设置推出bvc时,底部区域隐藏
1790     bvc.hidesBottomBarWhenPushed=YES;
1791     [self.navigationController pushViewController: bvc animated:YES];
1792 }
1793 
1794      4.特效按钮:木棍(固定的)  调整工具栏的距离,一般放在最左、最右。可以设置宽度width
1795           UIBarButtonSystemItemFixedSpace
1796    5.特效按钮:弹簧(随着之间的距离自动调整)  放在两个对象之间
1797 
1798 例:暂停,前进,后退按钮显示在工具栏中。
1799 - (void)viewDidLoad
1800 {
1801     [super viewDidLoad];
1802     //将默认隐藏的工具栏显示出来
1803     self.navigationController.toolbarHidden = NO;
1804     //配置工具栏
1805     UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil];
1806     UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nil action:nil];
1807     UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:nil action:nil];
1808     //特效按钮:木棍
1809     UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
1810     item4.width = 40;
1811     //特效按钮:弹簧
1812     UIBarButtonItem *item5 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
1813     
1814     self.toolbarItems = @[item4,item2,item5,item1,item5,item3,item4];
1815 }
1816 
1817 @end
1818 4.多个导航之间切换
1819 是当前界面的导航跳转到另一个界面的导航,导航与导航之间的切换(从下面推出)。此时就不用push 和pop 来推出和返回了(从左面推出)
1820 1.推出时修改为:
1821 UINavigationController*navi2=[[UINavigationController alloc]initWithRootViewController:bvc];
1822     [self.navigationController presentViewController:navi2 animated:YES completion:nil];
1823 2.返回到推出的界面
1824 [self.navigationController dismissViewControllerAnimated:YES completion:nil];
1825 
1826 小结:导航控制器可以设置的属性的作用于范围
1827                 属性:
1828                          .title
1829                          .navigationItem.leftBarButtonItem/s
1830                          .navigationItem.rightBarButtonItem/s
1831                          .toolbarItems
1832                  以上四个属性的设置只负责当前所属的vc
1833 
1834                 属性:
1835                          .navigationController.toolbarHidden
1836                     设置后,针对导航控制器管理的所有vc都生效
1837 
1838             *** 问:在不同的vc中都可以访问self.navigationController,那么是同一个导航控制器吗?
1839                         答:是
1840            推出的界面,
1841 ===============================================================================
1842 知识点
1843 十、UIImageView
1844 
1845 1.数据类型:NSString —>UILabel 显示
1846              UIImage—>UIImageView 显示
1847 
1848 2. 如何使用
1849 #import "MyViewController.h"
1850 
1851 @interface MyViewController ()
1852 
1853 @end
1854 
1855 @implementation MyViewController
1856 
1857 
1858 - (void)viewDidLoad
1859 {
1860     [super viewDidLoad];
1861     //创建图片
1862     UIImage *image = [UIImage imageNamed:@"Elephant.jpg"];//图片名称
1863     //创建图片控件,此时imageView的尺寸和创建时使用的图片的尺寸一样大
1864     UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
1865     //设置图片view的frame
1866     imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);//一定要设置尺寸,不然不会显示
1867 
1868     //设置imageView为圆角
1869     imageView.layer.cornerRadius = 30;
1870     imageView.layer.masksToBounds = YES;
1871     //设置图片view的内容显示方式
1872     imageView.contentMode = UIViewContentModeScaleAspectFit;
1873     //将图片控件添加到视图中
1874     [self.view addSubview:imageView];
1875 }
1876 
1877 @end
1878 3.拥有属性
1879             1.属性:
1880                 contentMode用于设置view内容显示的方式
1881                     1)UIViewContentModeScaleToFill:
1882                     修改宽高比,适应imageView的区域大小,图片会被拉伸,不留白边
1883                     2)UIViewContentModeScaleAspectFit:
1884                     维持宽高比不变的情况下,将整张图片可见,由于图片的宽高比和设置的imageView的frame的宽高比如果不协调的话,可能会留白边儿。
1885                     3)UIViewContentModeScaleAspectFill:
1886                     保持宽高比不变的情况下,将imageView所占的区域填满,所以只会显示图片的一部分,不会留白
1887                 
1888             2.属性:
1889                     layer.cornerRadius设置圆角的半径
1890                     layer.masksToBounds开启按边缘遮罩
1891 ===========================================================================
1892 知识点
1893 十一、UIScrollView
1894 
1895 1 .作用:
1896       在有限的区域内,显示更多的数据或图片
1897 
1898 2 .本质:
1899     管理view的view,scrollView本身没有任何的外观,依靠添加到scrollView中的其他视图来完成界面的显示
1900             
1901 3 .如何使用
1902 //imageView尺寸比较大,与图片一样大
1903           UIImageView* iV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Elephant.jpg"]];
1904     UIScrollView*sV=[[UIScrollView alloc]init];//创建scrollView的实例
1905     sV.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);//设置scrollView的frame
1906 
1907     [sV addSubview:iV];//将图片控件添加到scrollView中
1908     [self.view addSubview:sV];   //将滚动视图添加的view中                    
1909                 
1910 4.重要的属性:                
1911                         .frame 设置scrollView用多大的窗口来显示内容
1912                         .contentSize设置了可滚动的区域的大小
1913                         .contentOffset设置frame定点与内容的左顶点的偏移坐标
1914                         .contentInset设置内容与边界之间的上、左、下、右的距离
1915                     其他属性:
1916                         .bounces 是否可以边缘弹跳
1917                         .showsHorizontalScrollIndicator
1918                         .showsVerticalScrollIndicator
1919                         .indicatorStyle
1920   1. //设置scrollView可滚动查看的内容的区域大小
1921     scrollView.contentSize = imageView.frame.size;
1922 
1923     //设置滚动视图不可以弹跳
1924     scrollView.bounces = NO;
1925 
1926     //设置水平滚动条是否显示
1927     scrollView.showsHorizontalScrollIndicator = NO;
1928     //设置竖直滚动条是否显示
1929     scrollView.showsVerticalScrollIndicator = NO;
1930 
1931     //设置滚动条颜色
1932     scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
1933 
1934 2.//设置导航框,点击移动按钮,便可以跳到图片的设定范围
1935 self.title = @"scrollView";
1936     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"移动" style:UIBarButtonItemStyleDone target:self action:@selector(move)];
1937 -(void)move{
1938     self.sV.contentOffset=CGPointMake(1000, 1000);
1939     self.sV.contentInset = UIEdgeInsetsMake(300, 300, 300, 300);
1940 }
1941 
1942 5.如何实现滚动视图内容的缩放
1943                     step1:设置滚动内容缩放的最大比率
1944                     step2:设置滚动内容缩放的最小比率
1945                     step3:回答问题,说明scrollView里面的哪个子视图需要缩放
1946     //设置最大比率
1947     scrollView.maximumZoomScale = 1;
1948     CGFloat xScale = scrollView.frame.size.width/imageView.frame.size.width;
1949     CGFloat yScale = scrollView.frame.size.height/imageView.frame.size.height;
1950     //设置最小比率
1951     scrollView.minimumZoomScale = MIN(xScale, yScale);
1952     //设置当前控制器为scrollView的代理
1953     scrollView.delegate = self; **需遵守协议@interface MyViewController ()<UIScrollViewDelegate>
1954 
1955     //返回要缩放的视图  遵守协议
1956 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
1957     return self.imageView;
1958 }        
1959 6.UIPageControl 界面下方小圆点
1960     //创建pageControl
1961     UIPageControl *pageControl = [[UIPageControl alloc]init];
1962   
1963     pageControl.frame = CGRectMake(0, self.view.frame.size.height-20-30, self.view.frame.size.width, 30);
1964     pageControl.numberOfPages = self.imageNames.count;
1965 
1966     //设置圆点的颜色
1967     pageControl.pageIndicatorTintColor = [UIColor whiteColor];
1968     //设置被选中的圆点的颜色
1969     pageControl.currentPageIndicatorTintColor = [UIColor redColor];
1970 
1971     //关闭用户交互功能
1972     pageControl.userInteractionEnabled = NO;
1973     [self.view addSubview:pageControl];
1974 作业:
1975     
1976     1.视力检查器
1977             a。界面1:程序开始时,有一个检查视力的字母”E”一个Label显示E,font属性比较大.=[UIFont systemFontOfSize:100],界面下方有两个按钮,分别是看的清,看不清
1978             b。点击看的清,则推出第二个界面,第二个界面和第一个界面几乎一样,唯一不同的时E变小了,大小变成95
1979             c。点击看的清,继续推出第三个界面,E的字体继续变小,90 。。。 85 。。。 80.。。
1980             d。直到用户点击了看不清,告诉他,视力是多少
1981             要求:必须是使用UINavigationController来控制vc的跳转,每次应该推出新的vc对象,而不是修改原来的vc对象
1982 AppDelegate.h
1983 #import <UIKit/UIKit.h>
1984 
1985 @interface EViewController : UIViewController
1986 
1987 @property(nonatomic)CGFloat fontSize;//公开的属性,字母的大小
1988 
1989 @end
1990 AppDelegate.m
1991 #import "AppDelegate.h"
1992 #import "EViewController.h"
1993 
1994 @implementation AppDelegate
1995 
1996 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1997 {
1998     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
1999     EViewController *evc = [[EViewController alloc]initWithNibName:@"EViewController" bundle:nil];
2000     evc.fontSize = 100.0;
2001     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:evc];
2002     self.window.rootViewController = navi;
2003     [self.window makeKeyAndVisible];
2004     return YES;
2005 }
2006 EViewController.h
2007 EViewController.m
2008 #import "EViewController.h"
2009 
2010 @interface EViewController ()
2011 @property (weak, nonatomic) IBOutlet UILabel *label;
2012 
2013 @end
2014 
2015 @implementation EViewController
2016 
2017 
2018 
2019 - (void)viewDidLoad
2020 {
2021     [super viewDidLoad];
2022     self.title = [NSString stringWithFormat:@"%f",self.fontSize];
2023     self.label.font = [UIFont systemFontOfSize:self.fontSize];
2024    
2025 }
2026 - (IBAction)canSee:(id)sender {
2027     if ((self.fontSize-5)<0) {
2028         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"视力太好了" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
2029         [alert show];
2030         return;
2031     }
2032     EViewController *evc = [[EViewController alloc]initWithNibName:@"EViewController" bundle:nil];
2033     evc.fontSize = self.fontSize-10;
2034     [self.navigationController pushViewController:evc animated:YES];
2035 }
2036 
2037 - (IBAction)cannotSee:(id)sender {
2038     NSString *mes = [NSString stringWithFormat:@"视力是:%f",self.fontSize/100];
2039     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:mes delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
2040     [alert show];
2041 }
2042 
2043 @end
2044 
2045         2. 完成scrollView 对图片的缩放
2046 
2047 
2048         3.试做如 homework.png的效果图
2049 
2050 要求:四张图片拼接,通过滑动屏幕显示出来,同时手机下方有圆形按钮,随着滑动图片显示原点位置,同时在滑动到第四张图片时,添加一个按钮,点击按钮响应事件
2051 WelcomeViewController.h
2052 WelcomeViewController.m
2053 #import "AppDelegate.h"
2054 #import "WelcomeViewController.h"
2055 
2056 @implementation AppDelegate
2057 
2058 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2059 {
2060     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
2061     WelcomeViewController *vc = [[WelcomeViewController alloc]initWithNibName:@"WelcomeViewController" bundle:nil];
2062     self.window.rootViewController = vc;
2063     [self.window makeKeyAndVisible];
2064     return YES;
2065 }
2066 
2067 WelcomeViewController.h
2068 #import <UIKit/UIKit.h>
2069 
2070 @interface WelcomeViewController : UIViewController
2071 
2072 //图片的名称
2073 @property(nonatomic,strong)NSArray *imageNames;
2074 
2075 @end
2076 WelcomeViewController.m
2077 #import "WelcomeViewController.h"
2078 
2079 @interface WelcomeViewController ()<UIScrollViewDelegate>
2080 @property(nonatomic,strong)UIPageControl *pageControl;
2081 @end
2082 
2083 @implementation WelcomeViewController
2084 //重写初始化方法
2085 - (NSArray *)imageNames{
2086     if (!_imageNames) {
2087         _imageNames = @[@"welcome1.png",@"welcome2.png",@"welcome3.png",@"welcome4.png"];
2088     }
2089     return _imageNames;
2090 }
2091 
2092 - (void)viewDidLoad
2093 {
2094     [super viewDidLoad];
2095     UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
2096     //设置contentSize  大小:手机屏幕宽*4(图片的个数)
2097     scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*self.imageNames.count, scrollView.frame.size.height);
2098     
2099     //将所有图片以子视图的方式添加到scrollView中
2100     for (NSInteger i=0; i<self.imageNames.count; i++) {
2101         UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.imageNames[i]]];
2102         //创建一个frame变量,并赋值
2103         CGRect imageFrame = CGRectZero;
2104         imageFrame.size = scrollView.frame.size;
2105         imageFrame.origin.y = 0;
2106         imageFrame.origin.x = i*scrollView.frame.size.width;
2107         //将设置到好的frame变量给image
2108         imageView.frame = imageFrame;
2109         [scrollView addSubview:imageView];
2110     }
2111     
2112     //配置scrollView
2113     //设置整页滚动
2114     scrollView.pagingEnabled = YES;
2115     //设置边缘不弹跳
2116     scrollView.bounces = NO;
2117     //设置水平滚动条不显示
2118     scrollView.showsHorizontalScrollIndicator =NO;
2119     //设置scrollView的代理
2120     scrollView.delegate = self;
2121     
2122     [self.view addSubview:scrollView];
2123     
2124     
2125     //创建pageControl
2126     UIPageControl *pageControl = [[UIPageControl alloc]init];
2127     self.pageControl = pageControl;
2128     pageControl.frame = CGRectMake(0, self.view.frame.size.height-20-30, self.view.frame.size.width, 30);
2129     pageControl.numberOfPages = self.imageNames.count;
2130     //设置圆点的颜色
2131     pageControl.pageIndicatorTintColor = [UIColor whiteColor];
2132     //设置被选中的圆点的颜色
2133     pageControl.currentPageIndicatorTintColor = [UIColor redColor];
2134     //关闭用户交互功能
2135     pageControl.userInteractionEnabled = NO;
2136     [self.view addSubview:pageControl];
2137     
2138     //为最后一屏添加按钮
2139     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
2140     button.frame = CGRectMake(scrollView.frame.size.width*(self.imageNames.count-1), 0, scrollView.frame.size.width, scrollView.frame.size.height);
2141     //为按钮添加点击事件
2142     [button addTarget:self action:@selector(enterApp) forControlEvents:UIControlEventTouchUpInside];
2143     [scrollView addSubview:button];
2144 }
2145 //委托代理
2146 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
2147     // 获取移动的偏移定点坐标
2148     CGPoint offSet = scrollView.contentOffset;
2149     //根据坐标算出滚动到第几屏的位置下标
2150     NSInteger index = offSet.x/scrollView.frame.size.width;
2151     self.pageControl.currentPage=index;
2152 }
2153 
2154 -(void)enterApp{
2155     NSLog(@".........");
2156 }
2157 
2158 @end
2159 ===========================================================================
2160 知识点
2161 十二、表视图
2162   
2163 1.UITableView(表视图)
2164 
2165       1.1 什么是表视图?
2166              类型是:UITableView类
2167             表现形式:以一列多行的列表形式来展示数据的视图
2168             表视图的样式:普通表视图(Plain)    分组表视图(Group)
2169 
2170      1.2 表视图包含的部分?
2171             +UITableView
2172                     +TableHeaderView
2173                     +section1(分区)
2174                             +sectionHeader
2175                             +UITableViewCell (行、单元格)
2176                             +sectionFooter
2177                     +section2(分区)
2178                     +TableFooterView
2179             其中,表头、尾,分区头、尾可以根据需要选择的设置,但是在设定表格式,单元格是必须被设置的
2180 
2181    1.3 如何使用UITableView表视图?
2182         step1:创建tableView的实例,设置frame,以子视图的形式添加到self.view中
2183         step2:设置tableView的dataSource代理和delegate代理为当前控制器
2184         step3:设置控制器遵守UITableViewDataSource和UITableViewDelegate协议
2185         step4:实现dataSource协议中的三个方法用于设定表格展示的外观
2186                 方法1:用于设定整个表格有几个分区
2187                 方法2:用于设定每个分区有几行
2188                 方法3:用于设定每个行是什么样子
2189         step5:实现delegate协议中的一个方法用于设定表格可以对用户的点击某一行的动作的响应
2190             
2191         注:以上协议,dataSource协议必须遵守及实现方法2和方法3,方法1有默认的设置,可以根据需要修改;delegate协议在需要响应用户点击动作时再遵守和实现方法;另,实现表视图的过程可以简称为3问1答;3问指的是实现datasource协议中的三个方法,1答指的是实现delegate协议中的一个方法
2192 MyViewController.h
2193 #import <UIKit/UIKit.h>
2194 
2195 @interface MyViewController : UIViewController//继承自UIViewController
2196 
2197 @end
2198 MyViewController.m
2199 #import "MyViewController.h"
2200 
2201 @interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
2202 
2203 @end
2204 
2205 @implementation MyViewController
2206 
2207 - (void)viewDidLoad
2208 {
2209     [super viewDidLoad];
2210     //创建UITableView的实例
2211     UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame];
2212     //设置表视图的数据源代理
2213     tableView.dataSource = self;
2214     //设置表视图的代理
2215     tableView.delegate = self;
2216     //将表视图添加到view中
2217     [self.view addSubview:tableView];
2218 }
2219 
2220 
2221 //问1:表格有几个分区
2222 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
2223     return 1;
2224 }
2225 
2226 //问2:每个分区有几行
2227 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
2228     return 10;
2229 }
2230 
2231 //问3:每一行什么样
2232 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
2233     UITableViewCell *cell = [[UITableViewCell alloc]init];
2234     cell.textLabel.text = @"Hello World";
2235     return cell;
2236 }
2237 
2238 //答1:点击某一行后的响应
2239 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
2240     NSLog(@".....");
2241 }
2242 AppDelegate.h
2243 AppDelegate.m
2244 #import "AppDelegate.h"
2245 #import "MyViewController.h"
2246 
2247 @implementation AppDelegate
2248 
2249 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2250 {
2251     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
2252     MyViewController *vc = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
2253     self.window.rootViewController = vc;
2254     [self.window makeKeyAndVisible];
2255     return YES;
2256 }
2257 
2258 
2259 2.表视图控制器(UITableIViewController)
2260 
2261         2.1 是什么?
2262                 一种专门配置表视图的控制器
2263 
2264         2.2特点?
2265                 a)继承自UITableViewController
2266                 b)已经遵守了UITableViewDataSource和UITableViewDelegate协议
2267                 c)该控制器自带的视图已经是UITableView类型的了,并且可以借助于self.tableView属性来访问自带的这个表视图
2268                 d)控制器已经成为了自带的表视图的数据源代理对象和代理对象
2269 
2270         2.3 使用
2271                 创建表视图时,可以继承自UITableViewController,设置代理、遵守协议这些设置就都不需要单独完成了,只需要将关注点放在三问一答上即可。
2272 MyTableViewController.h
2273 #import <UIKit/UIKit.h>
2274 
2275 @interface MyTableViewController : UITableViewController//继承自UITableViewController
2276 
2277 @end
2278 MyTableViewController.m
2279 #import "MyTableViewController.h"
2280 #import "DetailViewController.h"
2281 
2282 @interface MyTableViewController ()
2283 
2284 @end
2285 
2286 @implementation MyTableViewController
2287 
2288 
2289 #pragma mark - 视图的生命周期    //方便从窗口快速定位到方法查看
2290 - (void)viewDidLoad
2291 {
2292     [super viewDidLoad];
2293     self.title = @"表格";
2294 }
2295 
2296 
2297 #pragma mark - dataSource协议
2298 
2299 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2300 {
2301     return 1;
2302 }
2303 
2304 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2305 {
2306     return 10;
2307 }
2308 
2309 
2310 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2311 {
2312     UITableViewCell *cell = [[UITableViewCell alloc]init];
2313     cell.textLabel.text = @"Hello Kitty";
2314     return cell;
2315 }
2316 #pragma mark - Table view delegate
2317 //一答
2318 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
2319 {
2320     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
2321     [self.navigationController pushViewController:detailViewController animated:YES];
2322 }
2323 
2324 @end
2325 DetailViewController.h
2326 DetailViewController.m
2327 
2328 AppDelegate.h
2329 AppDelegate.m
2330 #import "AppDelegate.h"
2331 #import "MyTableViewController.h"
2332 @implementation AppDelegate
2333 
2334 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2335 {
2336     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
2337     MyTableViewController *tvc = [[MyTableViewController alloc]initWithNibName:@"MyTableViewController" bundle:nil];
2338     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:tvc];
2339     self.window.rootViewController = navi;
2340     [self.window makeKeyAndVisible];
2341     return YES;
2342 }
2343 3.多分区的UITableView(表视图)
2344 
2345  3.1 NSIndexPath
2346          该类型描述的是一种路径,为了定位一个单元格的位置,需要两个值,一个是分区号,一个是在分区内的行号;分区号的排序规则从0开始,单元格在每一个分区内的排序规则也是从0开始。
2347         属性:
2348             .section 记录的是分区号
2349             .row    记录的是行在某一个分区内的行号
2350 MyTableViewController.h
2351 #import <UIKit/UIKit.h>
2352 
2353 @interface MyTableViewController : UITableViewController
2354 
2355 @end
2356 MyTableViewController.m
2357 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2358 {
2359     
2360     return 3;
2361 }
2362 
2363 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//分区号
2364 {   //第一个分区有三行
2365     if (section==0) {
2366         return 3;
2367     }else if (section==1){//第二个分区有两行
2368         return 2;
2369     }else{
2370       return 4;
2371     }
2372     
2373 }
2374 
2375 /**/
2376 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2377 {
2378     UITableViewCell *cell = [[UITableViewCell alloc]init];
2379     //每个区
2380     if (indexPath.section==0) {//第一个区
2381         if (indexPath.row==0) {//第一行
2382             cell.textLabel.text=@"hello";
2383         }else{
2384            cell.textLabel.text=@"Hello";
2385         }
2386         
2387     }else if (indexPath.section==1){//第二个分区所有行的内容
2388         cell.textLabel.text=@"hello wlord";
2389     }else{
2390        cell.textLabel.text=@"hello kity";
2391     }
2392     
2393     return cell;
2394 }
2395  3.2表头视图、表尾视图内容
2396 一个表格中,只能有一个表头和表尾视图
2397     通过以下两个属性进行设置:
2398                 .tableView.tableHeaderView
2399                 .tableView.tableFooterView
2400   - (void)viewDidLoad
2401 {
2402     [super viewDidLoad];
2403     //表头视图  可以在表头视图添加标签、文本框、按钮等
2404     UIView* headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];
2405     UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 100, 50)];//标签在视图中的位置
2406     label.backgroundColor=[UIColor greenColor];
2407     label.textAlignment=NSTextAlignmentCenter;//标签上的文字居中
2408     label.text=@"header";
2409     [headerView addSubview:label];//将标签添加到表视图
2410     self.tableView.tableHeaderView=headerView;
2411     //self.tableView.tableFooterView=headerView; 表尾视图
2412     
2413 }
2414   3.3 分区头、分区尾内容
2415          一个表格中,可以有多个分区头和分区尾,通过回答问题的方式进行设定;分区头和分区尾可以设置为简单的字符串描述,也可以设置复杂的UIView
2416 
2417  1)第一种设定方法
2418 //表头视图、表尾视图
2419 -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
2420     if (section==0) {
2421         return @"title1";//第一个分区表视图的表头内容
2422     }else if(section==1){
2423         return @"title2";
2424     }else{
2425        return @"title3";
2426     }
2427 }
2428 
2429  2)第二种设定方法
2430 -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
2431     //可以添加文本框、标签等
2432 }
2433 ========================================================================================================
2434 4.UITableViewCell 单元格
2435 
2436     4.1是什么?
2437         系统定义的单元格类型,表格中的每一个都是一个UITableViewCell的实例
2438 
2439     4.2单元格具有系统提供的默认的样式 
2440         不同的演示,对于系统默认提供的三个视图具有不同的摆放方式
2441 四种样式:
2442 UITableViewCellStyleDefault (imageView和textLabel在最左边,不显示detailTextLabel)
2443 UITableViewCellStyleValue1 (imageView和textLabel在最左边,detailTextLabel在最右边)
2444 UITableViewCellStyleValue2 (textLabel和detailTextLabel在一行,前边空几格,不显示图片)
2445 UITableViewCellStyleSubtitle (imageView最左边,detailTextLabel在textLabel的下方)
2446 
2447     4.3单元格具有系统提供的默认的组成视图
2448                     .textLabel  标题标签
2449                     .detailTextLabel 详情标签                     .imageView 图片视图
2450 //每行的内容
2451 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2452 {
2453     UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
2454     cell.textLabel.text=@"title";
2455     cell.detailTextLabel.text=@"title2";//详细的内容
2456     cell.imageView.image=[UIImage imageNamed:@"wifi.png"];
2457     return cell;
2458 }
2459   4.4设置单元格行高
2460 //设置单元格行高
2461 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
2462     return 70;
2463 }
2464 
2465 5.单元格的重用
2466 
2467     5.1 什么是单元格的重用?
2468          将完整超出屏幕的单元格存到队列中,屏幕一旦出现空白区域,需要单元格来填充时,先去队列中按照指定的标示图来试着取,看有没有已经用完并存在队列中的单元格,有,就拿来修改值后重新加到界面中,从队列中取不出已用完的单元格时,则新建
2469 
2470    5.2 如何实现单元格的重用?
2471         前提:超出屏幕的单元格由系统自动窜到队列中    
2472         具体做的内容:在回答每行内容什么样的时候,先尝试着从队列中取单元格对象,取的结果有两种,要么取到,拿来继续用,要么取不到可重用的,那么自己新建即可
2473 
2474         ***注意:存到队列中的单元格可以有多种多样的,所以每一种进入队列的单元格都需要指定给一个标示,去队列中取单元格时,要说明按哪种标示来找一样的对象
2475 
2476 方法一:从队列中取cell对象,自己判断是否取到了cell,没有取到时,自己用代码创建cell对象
2477 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2478 {   //重复用单元格
2479     UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"abc"];
2480     if (cell==nil) {
2481         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"abc"];
2482     }
2483        cell.textLabel.text=@"hello";
2484     return cell;
2485 }
2486 方法二:viewDidLoad中提在注册单元格的类型,然后从队列中取出cell对象后,就算没有取到可重用的单元格,系统也会按照注册的cell样式创建新的cell对象
2487 - (void)viewDidLoad
2488 {
2489     [super viewDidLoad];
2490     //注册单元格
2491     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"abc"];
2492 }
2493 
2494 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2495 {   //重复用单元格
2496     UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"abc"];
2497     cell.textLabel.text=@"hello";
2498 
2499     return cell;
2500 }
2501    5.3如何进行表格的刷新,在原有的基础上添加
2502 
2503 方法一:整体刷新
2504 [self.tableView reloadData];
2505 方法二:局部刷新    
2506     //从第四行开始刷新 局部刷新                                                                                最后一行
2507     NSIndexPath* newIndexPath=[NSIndexPath indexPathForRow:self.citys.count-1 inSection:0];
2508 
2509     [self.tableView insertRowsAtIndexPaths:@[newIndexPath]
2510 withRowAnimation:UITableViewRowAnimationTop];
2511 
2512 6.三问一答中的一答
2513     6.1 推出简单的VC来显示数据详情
2514                     【Demo3_SimpleVC】
2515                     数据结构:
2516                                 + City                 : NSObject
2517                                 +name              : NSString
2518                                 +population    : NSInteger
2519 
2520     要求:
2521         a。有一组城市信息,以tableView的形式来展示所有城市的列表
2522         b。选中某一个城市后,推出新的普通vc,显示选中的城市名称和城市的人口数
2523 
2524 CityTableViewController.h
2525 #import <UIKit/UIKit.h>
2526 
2527 @interface CityTableViewController : UITableViewController
2528 
2529 @end
2530 CityTableViewController.m
2531 
2532 #import "CityTableViewController.h"
2533 #import "City.h"
2534 #import "DetailViewController.h"
2535 
2536 @interface CityTableViewController ()
2537 
2538 @property(nonatomic,strong)NSArray *citys;
2539 
2540 @end
2541 
2542 @implementation CityTableViewController
2543 //重写get方法
2544 - (NSArray *)citys{
2545     if (!_citys) {
2546         _citys = [City demoData];
2547     }
2548     return _citys;
2549 }
2550 
2551 
2552 
2553 
2554 - (void)viewDidLoad
2555 {
2556     [super viewDidLoad];
2557     self.title = @"城市列表";
2558 }
2559 
2560 
2561 #pragma mark - Table view data source
2562 
2563 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2564 {
2565     return 1;
2566 }
2567 
2568 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2569 {
2570     return self.citys.count;
2571 }
2572 
2573 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2574 {
2575     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
2576     if (cell == nil) {
2577         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
2578     }
2579     //根据row,去数组中取到city对象
2580     City *city = self.citys[indexPath.row];
2581     cell.textLabel.text = city.name;
2582     return cell;
2583 }
2584 
2585 #pragma mark - Table view delegate
2586 
2587 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
2588 {
2589     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
2590     //根据行号找到选中的城市对象
2591     City *city = self.citys[indexPath.row];
2592     //将city传给推出的vc来显示
2593     detailViewController.city = city;
2594     [self.navigationController pushViewController:detailViewController animated:YES];
2595 }
2596 @end
2597 DetailViewController.h
2598 #import <UIKit/UIKit.h>
2599 #import "City.h"
2600 
2601 @interface DetailViewController : UIViewController
2602 @property(nonatomic,strong)City *city;
2603 
2604 @end
2605 DetailViewController.m
2606 #import "DetailViewController.h"
2607 
2608 @interface DetailViewController ()
2609 @property (weak, nonatomic) IBOutlet UILabel *nameLabel; 连线
2610 @property (weak, nonatomic) IBOutlet UILabel *populationLabel;
2611 
2612 @end
2613 
2614 @implementation DetailViewController
2615 
2616 
2617 - (void)viewDidLoad
2618 {
2619     [super viewDidLoad];
2620     self.title = @"城市详情";
2621 }
2622 
2623 - (void)viewWillAppear:(BOOL)animated{
2624     [super viewWillAppear:animated];
2625     self.nameLabel.text = self.city.name;
2626     self.populationLabel.text = [NSString stringWithFormat:@"%d万",self.city.population];
2627 }
2628 
2629 @end
2630 
2631 City.h
2632 #import <Foundation/Foundation.h>
2633 
2634 @interface City : NSObject
2635 
2636 @property(nonatomic,strong)NSString *name;
2637 @property(nonatomic)NSInteger population;
2638 
2639 -(instancetype)initWithName:(NSString *)name andPopulation:(NSInteger)population;
2640 
2641 +(NSArray *)demoData;
2642 
2643 @end
2644 City.m
2645 #import "City.h"
2646 
2647 @implementation City
2648 
2649 -(instancetype)initWithName:(NSString *)name andPopulation:(NSInteger)population{
2650     self = [super init];
2651     if (self) {
2652         self.name = name;
2653         self.population = population;
2654     }
2655     return self;
2656 }
2657 
2658 + (NSArray *)demoData{
2659     City *t1 = [[City alloc]initWithName:@"北京" andPopulation:1000];
2660     City *t2 = [[City alloc]initWithName:@"上海" andPopulation:900];
2661     City *t3 = [[City alloc]initWithName:@"广州" andPopulation:800];
2662     City *t4 = [[City alloc]initWithName:@"深圳" andPopulation:700];
2663     return @[t1,t2,t3,t4];
2664 }
2665 @end
2666  6.2 推出tableView来显示子数据
2667 
2668                 【Demo4_TableVC】
2669 
2670                     数据结构:
2671                                 + City                 : NSObject
2672                                 +name              : NSString
2673                                 +population    : NSInteger
2674                                 +areas(区域)   : NSArray (NSString)
2675                     例如:
2676                                 City:
2677                                         name:北京
2678                                         population:1000
2679                                         areas: @[@“东城区”,@“西城区”,@“朝阳区”]
2680                     要求:
2681                             a。有一组城市信息,以tableView的形式来展示所有城市的列表
2682                             b。选中某一个城市后,推出新的tableVC,显示选中的城市的所有areas信息
2683 City.h
2684 #import <Foundation/Foundation.h>
2685 
2686 @interface City : NSObject
2687 
2688 @property(nonatomic,strong)NSString *name;
2689 @property(nonatomic)NSInteger population;
2690 @property(nonatomic,strong)NSArray *areas;//地区
2691 
2692 -(instancetype)initWithName:(NSString *)name andPopulation:(NSInteger)population;
2693 
2694 +(NSArray *)demoData;
2695 
2696 @end
2697 City.m
2698 #import "City.h"
2699 
2700 @implementation City
2701 
2702 -(instancetype)initWithName:(NSString *)name andPopulation:(NSInteger)population{
2703     self = [super init];
2704     if (self) {
2705         self.name = name;
2706         self.population = population;
2707     }
2708     return self;
2709 }
2710 
2711 + (NSArray *)demoData{
2712     City *t1 = [[City alloc]initWithName:@"北京" andPopulation:1000];
2713     t1.areas = @[@"东城区",@"西城区",@"海淀区"];
2714     
2715     City *t2 = [[City alloc]initWithName:@"上海" andPopulation:900];
2716     t2.areas = @[@"浦东区",@"静安区",@"徐汇区"];
2717     
2718     City *t3 = [[City alloc]initWithName:@"广州" andPopulation:800];
2719     t3.areas = @[@"白云区",@"越秀区",@"天河区"];
2720     
2721     City *t4 = [[City alloc]initWithName:@"深圳" andPopulation:700];
2722     t4.areas = @[@"未知区",@"未知2区"];
2723     
2724     return @[t1,t2,t3,t4];
2725 }
2726 @end
2727 CityTableViewController.h
2728 CityTableViewController.m
2729 同上
2730 DetailViewController.h
2731 #import <UIKit/UIKit.h>
2732 #import "City.h"
2733 
2734 @interface DetailViewController : UITableViewController ***继承
2735 @property(nonatomic,strong)City *city;
2736 
2737 @end
2738 DetailViewController.m
2739 #import "DetailViewController.h"
2740 
2741 @interface DetailViewController ()
2742 
2743 @end
2744 
2745 @implementation DetailViewController
2746 
2747 - (void)viewDidLoad
2748 {
2749     [super viewDidLoad];
2750     self.title = self.city.name;
2751     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
2752 }
2753 
2754 
2755 #pragma mark - Table view data source
2756 
2757 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2758 {
2759     return 1;
2760 }
2761 
2762 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2763 {
2764     return self.city.areas.count;
2765 }
2766 
2767 
2768 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2769 {
2770     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
2771     cell.textLabel.text = self.city.areas[indexPath.row];
2772     return cell;
2773 }
2774 
2775 
2776   6.3 使用多分区来显示子数据                 【Demo5_MutilSection】
2777 
2778                     数据结构:
2779                                 + City                 : NSObject
2780                                 +name              : NSString
2781                                 +population    : NSInteger
2782                                 +areas(区域)   : NSArray (NSString)
2783                     例如:
2784                                 City:
2785                                         name:北京
2786                                         population:1000
2787                                         areas: @[@“东城区”,@“西城区”,@“朝阳区”]
2788                     要求:
2789                     a。使用一个tableView的多分区展示城市名称及子地区名称
2790                     b。每个分区的头,显示城市名称
2791                     c。每个分区的尾,显示城市的人口
2792                     d。分区内的多行显示城市的所有areas
2793 City.h
2794 City.m
2795 同上
2796 CityTableViewController.h
2797 CityTableViewController.m
2798 #import "CityTableViewController.h"
2799 #import "City.h"
2800 
2801 @interface CityTableViewController ()
2802 
2803 @property(nonatomic,strong)NSArray *citys;
2804 
2805 @end
2806 
2807 @implementation CityTableViewController
2808 
2809 - (NSArray *)citys{
2810     if (!_citys) {
2811         _citys = [City demoData];
2812     }
2813     return _citys;
2814 }
2815 
2816 
2817 - (void)viewDidLoad
2818 {
2819     [super viewDidLoad];
2820     self.title = @"城市列表";
2821 }
2822 
2823 
2824 #pragma mark - Table view data source
2825 
2826 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2827 {
2828     //有几个城市就是几个分区
2829     return self.citys.count;
2830 }
2831 
2832 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2833 {
2834     //每个城市的子地区有几个,那么该分区就有几行
2835     City *city = self.citys[section];
2836     return city.areas.count;
2837 }
2838 
2839 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2840 {
2841     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
2842     if (cell == nil) {
2843         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
2844     }
2845     //根据section找到对应的city
2846     City *city = self.citys[indexPath.section];
2847     cell.textLabel.text = city.areas[indexPath.row];
2848     return cell;
2849 }
2850 
2851 //设置section头
2852 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
2853     City *city = self.citys[section];
2854     return city.name;
2855 }
2856 
2857 //设置section尾
2858 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
2859     City *city = self.citys[section];
2860     return [NSString stringWithFormat:@"人口数:%d万",city.population];
2861 }
2862 
2863 @end
2864 
2865  6.4 向TableView中添加一行数据
2866             数据模型:
2867                                 + City                 : NSObject
2868                                 +name              : NSString
2869                                 +population    : NSInteger
2870             要求:
2871                     a。第一个界面使用tableView展示所有的城市信息
2872                     b。城市名称在单元格的左侧显示
2873                     c。城市的人口在单元格的右侧显示
2874                     d。在导航栏的右上角有一个加号按钮,点击这个加号后,推出一个新的普通vc
2875                     e。在推出的界面2中,包含两个文本框,一个用于输入新的城市名称,一个用于输入该城市的人口数
2876                     f。界面2中下方有一个保存按钮,点击保存按钮后,返回到界面1,并且将在界面2中输入的数据回传到界面1,保存在界面1中用于存放所有城市信息的数组中
2877                     g。同时,更新表格,显示增加完城市信息后的新数据(注:如何刷新整个表格?[ self.tableView reloadData];)
2878 TRCity.h
2879 #import <Foundation/Foundation.h>
2880 
2881 @interface TRCity : NSObject
2882 @property(nonatomic,strong)NSString* name;
2883 @property(nonatomic)NSInteger population;
2884 
2885 -(instancetype)initWithName:(NSString*)name andPopulation:(NSInteger)population;
2886 
2887 +(NSMutableArray*)cityArrays;
2888 @end
2889 TRCity.m
2890 #import "TRCity.h"
2891 
2892 @implementation TRCity
2893 -(instancetype)initWithName:(NSString*)name andPopulation:(NSInteger)population{
2894     if ([super init]) {
2895         self.name=name;
2896         self.population=population;
2897     }
2898     return self;
2899 }
2900 
2901 +(NSMutableArray*)cityArrays{
2902     TRCity* c1=[[TRCity alloc]initWithName:@"北京" andPopulation:1000];
2903     TRCity* c2=[[TRCity alloc]initWithName:@"上海" andPopulation:800];
2904     TRCity* c3=[[TRCity alloc]initWithName:@"广州" andPopulation:600];
2905     return [@[c1,c2,c3]mutableCopy];
2906 }
2907 @end
2908 
2909 CityTableViewController.h
2910 #import <UIKit/UIKit.h>
2911 
2912 @interface CityTableViewController : UITableViewController
2913 
2914 @end
2915 CityTableViewController.m
2916 #import "CityTableViewController.h"
2917 #import "TRCity.h"
2918 #import "MyViewController.h"
2919 
2920 @interface CityTableViewController ()<MyViewControllerDelegate>
2921 @property(nonatomic,strong)NSMutableArray* citys;
2922 //@property(nonatomic,strong)NSMutableArray*message;
2923 @end
2924 
2925 @implementation CityTableViewController
2926 
2927 -(NSMutableArray*)citys{
2928     if (!_citys) {
2929         _citys=[TRCity cityArrays];
2930     }
2931     return _citys;
2932 }
2933 
2934 //添加导航栏配置
2935 - (void)viewDidLoad
2936 {
2937     [super viewDidLoad];
2938     
2939      self.title=@"城市列表";
2940     UIBarButtonItem* r=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: target:self action:@selector(gotoNewVC)];
2941     self.navigationItem.rightBarButtonItem=r;
2942     
2943 }
2944 //点击加号切换到的界面
2945 -(void)gotoNewVC{
2946     MyViewController* myVC=[[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
2947     [self.navigationController pushViewController:myVC animated:YES  ];
2948     myVC.delegate=self;
2949     
2950 }
2951 
2952 //实现方法
2953 -(void)myViewController:(MyViewController *)mVC gobackMessage:(NSArray *)message{
2954     //self.citys=message;
2955     TRCity* city=[[TRCity alloc]init];
2956     
2957     city.name=message[0];
2958     city.population=[message[1] integerValue];
2959     [self.citys addObject:city];
2960     //刷新表格 从原有的刷新
2961     //[ self.tableView reloadData];
2962     
2963     //从第四行开始刷新 局部刷新
2964     NSIndexPath* newIndexPath=[NSIndexPath indexPathForRow:self.citys.count-1 inSection:0];
2965     [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
2966     
2967 }
2968 
2969 
2970 #pragma mark - Table view data source
2971 
2972 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2973 {
2974 
2975     return 1;
2976 }
2977 
2978 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2979 {
2980     
2981     return self.citys.count;
2982 }
2983 
2984 /**/
2985 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2986 {
2987     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
2988     if (cell==nil) {
2989         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
2990     }
2991     
2992     TRCity* city=self.citys[indexPath.row];
2993     cell.textLabel.text=city.name;
2994     cell.detailTextLabel.text=[NSString stringWithFormat:@"人口:%d",city.population];
2995     
2996     
2997     return cell;
2998 }
2999 MyViewController.h
3000 #import <UIKit/UIKit.h>
3001 //定义协议
3002 @class MyViewController;
3003 @protocol MyViewControllerDelegate <NSObject>
3004 
3005 -(void)myViewController:(MyViewController*)mVC gobackMessage:(NSArray*)message;
3006 
3007 @end
3008 
3009 @interface MyViewController : UIViewController
3010 
3011 @property(nonatomic,weak)id<MyViewControllerDelegate>delegate;
3012 @end
3013 MyViewController.m
3014 #import "MyViewController.h"
3015 
3016 @interface MyViewController ()
3017 
3018 @property (weak, nonatomic) IBOutlet UITextField *name; 连线文本框
3019 @property (weak, nonatomic) IBOutlet UITextField *population;
3020 @property(nonatomic,strong)NSArray* city;
3021 @end
3022 
3023 @implementation MyViewController
3024 
3025 - (void)viewDidLoad
3026 {
3027     [super viewDidLoad];
3028     
3029 }
3030 - (IBAction)returnCS:(UITextField *)sender {
3031     [self.view endEditing:YES];
3032 }
3033 - (IBAction)returnRK:(UITextField *)sender {
3034     [self.view endEditing:YES];
3035 }
3036 //点击保存按钮返回到界面1
3037 - (IBAction)goBackCIty:(UIButton *)sender {
3038     NSString* str=self.name.text;
3039     NSString*str2=self.population.text;
3040     self.city=@[str,str2];
3041     [self.delegate myViewController:self gobackMessage:self.city];
3042     [self.navigationController popToRootViewControllerAnimated:YES];
3043 }
3044 
3045 @end
3046 
3047 7.表格的编辑模式
3048 
3049    1.1 什么是表格的编辑模式?
3050         在表格上可以进行cell的删除、增加、移动的操作
3051    1.2 如何实现数据的编辑(删除、增加)
3052         实现步骤:
3053                 a。启动表格的编辑模式:通过修改tableView的editing属性即可
3054                 b。回答 两问一答  三个问题
3055                         问1:哪些行可以进入编辑模式
3056                         问2:行处于何种编辑模式
3057                         答1:点击编辑按钮后要做的响应
3058 
3059   1.3 实现数据删除或增加时,一定是先改数据模型,然后再刷新界面
3060 例:
3061 
3062    1).启用编辑模式两种方法
3063 - (void)viewDidLoad
3064 {
3065     [super viewDidLoad];
3066     self.title = @"联系人";
3067    ****//创建启用编辑按钮的方式一
3068     //self.navigationItem.rightBarButtonItem = self.editButtonItem;
3069     
3070    ****//创建启用编辑按钮的方式二
3071 
3072    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(tableBeginEditing)];
3073 }
3074 
3075 -(void)tableBeginEditing{
3076     //self.tableView.editing = YES;
3077     //启动表格的编辑模式
3078     [self.tableView setEditing:!self.tableView.editing animated:YES];
3079     //修改右侧按钮上的显示文字
3080     if (self.tableView.editing) {
3081         [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
3082     }else {
3083         [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
3084     }
3085     
3086 }
3087   2).利用启用编辑模式进行删除、添加
3088 MyTableViewController.h
3089 MyTableViewController.m
3090 #import "MyTableViewController.h"
3091 
3092 @interface MyTableViewController ()
3093 @property(nonatomic,strong)NSMutableArray *names;
3094 
3095 @end
3096 
3097 @implementation MyTableViewController
3098 
3099 - (NSMutableArray *)names{
3100     if (!_names) {
3101         _names = [@[@"张三",@"李四",@"王五",@"赵六"] mutableCopy];
3102     }
3103     return _names;
3104 }
3105 
3106 
3107 
3108 - (void)viewDidLoad
3109 {
3110     [super viewDidLoad];
3111     self.title = @"联系人";
3112     //创建启用编辑按钮的方式一
3113     //self.navigationItem.rightBarButtonItem = self.editButtonItem;
3114     
3115     //创建启用编辑按钮的方式二
3116     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(tableBeginEditing)];
3117 }
3118 
3119 -(void)tableBeginEditing{
3120     //self.tableView.editing = YES;
3121     //启动表格的编辑模式
3122     [self.tableView setEditing:!self.tableView.editing animated:YES];
3123     //修改右侧按钮上的显示文字
3124     if (self.tableView.editing) {
3125         [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
3126     }else {
3127         [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
3128     }
3129     
3130 }
3131 
3132 #pragma mark - Table view data source
3133 
3134 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3135 {
3136     return 1;
3137 }
3138 
3139 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3140 {
3141     return self.names.count;
3142 }
3143 
3144 
3145 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3146 {
3147     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
3148     if (cell == nil) {
3149         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
3150     }
3151     cell.textLabel.text = self.names[indexPath.row];
3152     return cell;
3153 }
3154 
3155 #pragma mark - table editing
3156 
3157 //问1:该行是否可以进入编辑模式
3158 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
3159         return YES;
3160 }
3161 
3162 //问2:该行使用什么编辑样式
3163 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
3164     if (indexPath.row == self.names.count-1){
3165         return UITableViewCellEditingStyleInsert;
3166     }else{
3167         return UITableViewCellEditingStyleDelete;
3168     }
3169 }
3170 
3171 //答1:确定编辑动作后的响应
3172 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
3173     //如果提交的编辑样式是删除动作时
3174     if (editingStyle == UITableViewCellEditingStyleDelete) { 删除模式
3175         //1.根据选择的行的位置,修改数据模型
3176         [self.names removeObjectAtIndex:indexPath.row];
3177         //2.刷新tableView
3178         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
3179     }else if(editingStyle == UITableViewCellEditingStyleInsert){ 添加模式
3180         //1.修改数据模型
3181         [self.names addObject:@"test"];
3182         //2.刷新tableView
3183         NSIndexPath *newPath = [NSIndexPath indexPathForRow:self.names.count-1 inSection:0];
3184         [self.tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationRight];
3185     }
3186 }
3187 @end
3188     1.4 实现数据的移动
3189 //移动
3190 //一问
3191 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
3192     return YES;
3193 }
3194 //一答
3195 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
3196     //建立模型
3197     //获取要移动的数据对象
3198     NSString* str=[self.names objectAtIndex:sourceIndexPath.row];
3199     //将数据从数组中移除
3200     [self.names removeObjectAtIndex:sourceIndexPath.row];
3201     //按照新的坐标位置,将对象在插入回数组中
3202     [self.names insertObject:str atIndex:destinationIndexPath.row];
3203 }
3204 
3205 8. 单元格的contentView内容视图
3206       UITableViewCell继承自UIView,其中又包含了左右两个区域,其中左侧用于显示内容的区域叫做内容视图,想访问这个区域,可以通过cell.contentView属性即可
3207                 a)系统为内容视图提供了默认的三个控件
3208                     .textLabel
3209                     .detailTextLabel
3210                     .imageView
3211                 b)可以自定义内容视图
3212                      [cell.contentView addSubView:xxx]
3213 MyTableViewController.h
3214 MyTableViewController.m
3215 #import "MyTableViewController.h"
3216 
3217 @interface MyTableViewController ()
3218 
3219 @property(nonatomic,strong)NSArray *citys;
3220 
3221 @end
3222 
3223 @implementation MyTableViewController
3224 
3225 - (NSArray *)citys{
3226     if (!_citys) {
3227         _citys = @[@"北京",@"上海",@"广州",@"深圳",@"杭州",@"苏州",@"厦门",@"天津",@"重庆",@"呼和浩特",@"郑州",@"乌鲁木齐",@"拉萨",@"xx",@"yy",@"zz",@"mm",@"nn",@"qq",@"aa",];
3228     }
3229     return _citys;
3230 }
3231 
3232 
3233 
3234 - (void)viewDidLoad
3235 {
3236     [super viewDidLoad];
3237 }
3238 
3239 
3240 #pragma mark - Table view data source
3241 
3242 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3243 {
3244     return 1;
3245 }
3246 
3247 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3248 {
3249     return self.citys.count;
3250 }
3251 
3252 
3253 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3254 {
3255     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" ];
3256     
3257     UILabel *label = nil;
3258     //经过一些步骤,label指向一个对象
3259     if (cell == nil) {
3260         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
3261         label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
3262         label.font = [UIFont systemFontOfSize:22];
3263         label.shadowColor = [UIColor redColor];
3264         label.shadowOffset = CGSizeMake(2, 2);
3265         label.textAlignment = NSTextAlignmentCenter;
3266         //为label添加一个容器内的对象标识
3267         label.tag = 1;
3268         [cell.contentView addSubview:label];
3269     }else{
3270         //获取cell中已经添加了的那个label
3271         label = (UILabel *)[cell.contentView viewWithTag:1];
3272     }
3273     label.text = self.citys[indexPath.row];
3274     return cell;
3275 }
3276 
3277 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
3278     return 60;
3279 }
3280 
3281 @end
3282 
3283 9.单元格的accessoryView(辅助视图)
3284 
3285            a)使用系统提供的辅助视图样式
3286                      cell.accessoryType
3287                 UITableViewCellAccessoryCheckmark:对勾
3288                 UITableViewCellAccessoryDisclosureIndicator:大于号
3289                 UITableViewCellAccessoryDetailButton:圆圈i
3290                 UITableViewCellAccessoryDetailDisclosureButton:圆圈i+大于号
3291                 注意:包含detail按钮时,cell可以有两种不同的单击事件的响应。
3292                          点击圆圈i部分:响应accessoryButtonTapped:方法
3293                       点击圆圈i以外的部分:响应didSelectRowAtIndexPath:
3294   如:  cell.accessoryType = UITableViewCellAccessoryDetailButton;
3295 
3296         b)自定义辅助视图
3297                      cell.accessoryView = [UIButton]
3298              如:cell.accessoryView = [[UISwitch alloc]init];
3299 MyTableViewController.h
3300 MyTableViewController.m
3301 #import "MyTableViewController.h"
3302 
3303 @interface MyTableViewController ()
3304 
3305 @end
3306 
3307 @implementation MyTableViewController
3308 
3309 - (void)viewDidLoad
3310 {
3311     [super viewDidLoad];
3312 }
3313 #pragma mark - Table view data source
3314 
3315 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3316 {
3317     return 1;
3318 }
3319 
3320 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3321 {
3322     return 5;
3323 }
3324 
3325 static NSString *cellIdentifier = @"cell";
3326 
3327 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3328 {
3329     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
3330     if (cell==nil) {
3331         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
3332     }
3333     if (indexPath.row == 2) {
3334         //系统定义的辅助视图样式
3335         cell.accessoryType = UITableViewCellAccessoryDetailButton;
3336         //自定义辅助视图样式
3337     }else if (indexPath.row == 3) {
3338         cell.accessoryView = [[UISwitch alloc]init];
3339     }else if(indexPath.row == 1){
3340         UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
3341         [button setTitle:@"验证码" forState:UIControlStateNormal];
3342         button.frame = CGRectMake(0, 0, 100, 40);
3343         cell.accessoryView = button;
3344     }
3345     cell.textLabel.text = @"Hello World";
3346     return cell;
3347 }
3348 
3349 //一响应
3350 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
3351     NSLog(@"didSelectRowAtIndexPath");
3352 }
3353 
3354 //响应用户点击圆圈i (Detail按钮)
3355 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
3356     NSLog(@"....");
3357 }
3358 
3359 @end
3360 10. 自定义单元格
3361     1.预备:
3362         根据要显示的结果,分析底层的数据模型,创建模型类,cell类是模型类的一种显示外观
3363     2.实现步骤:
3364                 step1:编写一个类,继承自UITableViewCell
3365                                 勾选xib
3366                //step2:创建可以进行界面设置的xib文件,设计界面
3367                //step3:将xib文件与自己编写的Cell类关联起来
3368                 step4:将xib中设计的各个可被修改的控件进行连线,且要变成公开的属性
3369                 step5:创建表格,回答第三问时,不再创建UITableViewCell的实例,而是创建我们自己编写的cell类的实例,返回即可。
3370 News.h
3371 #import <Foundation/Foundation.h>
3372 
3373 @interface News : NSObject
3374 
3375 @property(nonatomic,strong)NSString *title;
3376 @property(nonatomic,strong)NSString *newsImageName;
3377 @property(nonatomic)NSUInteger commentNumber;
3378 
3379 -(instancetype)initWithTitle:(NSString *)title andImageName:(NSString *)imageName andCommentNumber:(NSUInteger)number;
3380 +(NSArray *)demoData;
3381 
3382 @end
3383 
3384 News.m
3385 #import "News.h"
3386 
3387 @implementation News
3388 
3389 -(instancetype)initWithTitle:(NSString *)title andImageName:(NSString *)imageName andCommentNumber:(NSUInteger)number{
3390     self = [super init];
3391     if (self) {
3392         self.title = title;
3393         self.newsImageName = imageName;
3394         self.commentNumber = number;
3395     }
3396     return self;
3397 }
3398 
3399 + (NSArray *)demoData{
3400     News *n1 = [[News alloc]initWithTitle:@"xxx" andImageName:@"icon40.png" andCommentNumber:200];
3401     News *n2 = [[News alloc]initWithTitle:@"yyyyyyy" andImageName:@"icon40.png" andCommentNumber:100];
3402     News *n3 = [[News alloc]initWithTitle:@"zzzzzzz" andImageName:@"icon40.png" andCommentNumber:20];
3403     News *n4 = [[News alloc]initWithTitle:@"mmmmmm" andImageName:@"icon40.png" andCommentNumber:0];
3404     return @[n1,n2,n3,n4];
3405 }
3406 @end
3407 
3408 NewsCell.h
3409 #import <UIKit/UIKit.h>
3410 
3411 @interface NewsCell : UITableViewCell //继承自
3412 
3413 @property (weak, nonatomic) IBOutlet UIImageView *newsImageView;
3414 @property (weak, nonatomic) IBOutlet UILabel *title;
3415 @property (weak, nonatomic) IBOutlet UILabel *commentNumberLabel;
3416 @end
3417 NewsCell.m
3418 #import "NewsCell.h"
3419 
3420 @implementation NewsCell
3421 
3422 @end
3423 NewsTableViewController.h
3424 #import <UIKit/UIKit.h>
3425 
3426 @interface NewsTableViewController : UITableViewController
3427 @property(nonatomic,strong)NSArray *allNews;
3428 @end
3429 NewsTableViewController.m
3430 #import "NewsTableViewController.h"
3431 #import "News.h"
3432 #import "NewsCell.h"
3433 
3434 @interface NewsTableViewController ()
3435 @end
3436 
3437 @implementation NewsTableViewController
3438 
3439 static NSString *cellIdentifier = @"cell";
3440 
3441 - (void)viewDidLoad
3442 {
3443     [super viewDidLoad];
3444     //注册NewsCell类型,重用时由系统创建指定的类型
3445     //由于自定义的cell类带有xib文件
3446     //所以注册时使用nib方法,说明,如果系统
3447     //帮助创建cell对象,则根据指定的xib文件来创建
3448     [self.tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
3449 }
3450 
3451 
3452 #pragma mark - Table view data source
3453 
3454 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3455 {
3456     return 1;
3457 }
3458 
3459 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3460 {
3461     return self.allNews.count;
3462 }
3463 
3464 
3465 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3466 {
3467     NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
3468     //按下标找到新闻对象
3469     News *news = self.allNews[indexPath.row];
3470     cell.title.text = news.title;
3471     cell.newsImageView.image = [UIImage imageNamed:news.newsImageName];
3472     cell.commentNumberLabel.text = [NSString stringWithFormat:@"%d",news.commentNumber];
3473     return cell;
3474 }
3475 //设置行高
3476 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
3477     return 60;
3478     
3479 }
3480 
3481 @end
3482             
3483 作业:
3484 见下发的资源包
3485         
3486 
3487 
3488 1.qq音乐的配置界面
3489 MyTableViewController.h
3490 MyTableViewController.m
3491 #import "MyTableViewController.h"
3492 
3493 @interface MyTableViewController ()
3494 
3495 @end
3496 
3497 @implementation MyTableViewController
3498 
3499 - (void)viewDidLoad
3500 {
3501     [super viewDidLoad];
3502     
3503     self.title=@"更多";
3504     
3505     UIView* headeView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 70)];
3506     
3507     UILabel* label1=[[UILabel alloc]initWithFrame:CGRectMake(15, 40, 80, 30)];
3508     label1.text=@"关注 3";
3509     
3510     UILabel* label2=[[UILabel alloc]initWithFrame:CGRectMake(130, 40, 160, 30)];
3511     label2.text=@"粉丝 0";
3512     
3513     UILabel* label3=[[UILabel alloc]initWithFrame:CGRectMake(240, 40, 240, 30)];
3514     label3.text=@"动态 0";
3515     
3516     UILabel* label4=[[UILabel alloc]initWithFrame:CGRectMake(240, 20, 240, 20)];
3517     label4.text=@"我的绿钻";
3518     label4.textColor=[UIColor greenColor];
3519 
3520     [headeView addSubview:label1];
3521     [headeView addSubview:label2];
3522     [headeView addSubview:label3];
3523     [headeView addSubview:label4];
3524     
3525     UILabel* label5=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, 90, 30)];
3526     label5.text=@"傻丫头";
3527     [headeView addSubview:label5];
3528     //照片
3529     UIImage* image=[UIImage imageNamed:@"header.png"];
3530     UIImageView* imageView=[[UIImageView alloc]initWithImage:image];
3531     imageView.frame=CGRectMake(10, 0, 40, 40);
3532     [headeView addSubview:imageView];
3533     imageView.layer.cornerRadius=20;
3534     imageView.layer.masksToBounds=YES;
3535     
3536     self.tableView.tableHeaderView=headeView;
3537     
3538 }
3539 
3540 
3541 #pragma mark - Table view data source
3542 
3543 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3544 {
3545 
3546     return 3;
3547 }
3548 
3549 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3550 {
3551     if (section==0) {
3552         return 4;
3553     }else if (section==1){
3554         return 3;
3555     }else{
3556         return 1;
3557     }
3558 }
3559 
3560 /**/
3561 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3562 {
3563     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
3564     if (cell==nil) {
3565         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
3566     }
3567     cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
3568     
3569     if (indexPath.section==0) {
3570         if (indexPath.row==0) {
3571             cell.textLabel.text=@"我的绿钻";
3572         }else if (indexPath.row==1){
3573             cell.textLabel.text=@"免流量服务";
3574             cell.detailTextLabel.text=@"在线听歌免流量费";
3575         }else if (indexPath.row==2){
3576             cell.textLabel.text=@"QPlay与车载互联";
3577             cell.detailTextLabel.text=@"开启";
3578         }else{
3579             cell.textLabel.text=@"笛音传歌";
3580             cell.detailTextLabel.text=@"声波传送,一触即发";
3581         }
3582     }else if (indexPath.section==1){
3583         if (indexPath.row==0) {
3584             cell.textLabel.text=@"设置";
3585         }else if (indexPath.row==1){
3586             cell.textLabel.text=@"定时关闭";
3587             //设置辅视图
3588             cell.accessoryView=[[UISwitch alloc]init];
3589         }else if (indexPath.row==2){
3590             cell.textLabel.text=@"关于QQ音乐";
3591         }
3592     }else{
3593         cell.textLabel.text=@"退出登录";
3594         cell.textLabel.textColor=[UIColor redColor];
3595        // cell.textLabel.textAlignment=NSTextAlignmentCenter;
3596     }
3597     
3598     return cell;
3599 }
3600 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
3601     return 40;
3602 }
3603 
3604 -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
3605     
3606         return @" ";
3607     
3608 }
3609 
3610 
3611 2.新闻客户端的首页,新闻展示
3612 News.h
3613 #import <Foundation/Foundation.h>
3614 
3615 @interface News : NSObject
3616 
3617 @property(nonatomic,strong) NSString *title;
3618 @property(nonatomic,strong) NSString *newsImageName;
3619 @property(nonatomic) NSUInteger commentNumber;
3620 
3621 -(instancetype)initWithTitle:(NSString*)title
3622         andImageName:(NSString*)imageName
3623     andCommentNumber:(NSUInteger)commentNumber;
3624 
3625 +(NSArray *)demoData;
3626 
3627 @end
3628 News.m
3629 #import "News.h"
3630 
3631 @implementation News
3632 
3633 -(instancetype)initWithTitle:(NSString*)title
3634         andImageName:(NSString*)imageName
3635     andCommentNumber:(NSUInteger)commentNumber{
3636     self = [super init];
3637     if (self) {
3638         self.title = title;
3639         self.newsImageName = imageName;
3640         self.commentNumber = commentNumber;
3641     }
3642     return self;
3643     
3644 }
3645 
3646 + (NSArray *)demoData{
3647     News *n1 = [[News alloc]initWithTitle:@"联通被曝高危漏洞 或致用户通话记录等信息泄露" andImageName:@"n1.png" andCommentNumber:186];
3648     News *n2 = [[News alloc]initWithTitle:@"CES2015回顾:民用无人机来袭 中国公司占主导" andImageName:@"n2.png" andCommentNumber:17];
3649     News *n3 = [[News alloc]initWithTitle:@"中企征战CES:难寻颠覆性产品 未打通海外品牌渠道" andImageName:@"n3.png" andCommentNumber:17];
3650     News *n4 = [[News alloc]initWithTitle:@"老话重提:“专车”是否是黑车?被查合不合法" andImageName:@"n4.png" andCommentNumber:178];
3651     News *n5 = [[News alloc]initWithTitle:@"马云告诫员工千万别碰京东:京东将会成悲剧" andImageName:@"n5.png" andCommentNumber:4374];
3652     News *n6 = [[News alloc]initWithTitle:@"三星Q4营业利润47亿美元超预期:内存芯片需求利好" andImageName:@"n6.png" andCommentNumber:6];
3653     News *n7 = [[News alloc]initWithTitle:@"索尼宣布PS4国行版延期上市 或因被举报不锁区" andImageName:@"n7.png" andCommentNumber:0];
3654     News *n8 = [[News alloc]initWithTitle:@"微众银行开业前夕推“信用付”临时核心系统过度" andImageName:@"n8.png" andCommentNumber:4];
3655     
3656     return @[n1,n2,n3,n4,n5,n6,n7,n8];
3657 }
3658 
3659 @end
3660 MyCell.xib
3661 3662 MyCell.h
3663 #import <UIKit/UIKit.h>
3664 
3665 @interface MyCell : UITableViewCell
3666 @property (weak, nonatomic) IBOutlet UIImageView *newsImageView; 与上图连线
3667 @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
3668 @property (weak, nonatomic) IBOutlet UILabel *commentNumberLabel;
3669 
3670 @end
3671 
3672 MyCell.m
3673 NewsViewController.h
3674 #import <UIKit/UIKit.h>
3675 
3676 @interface NewsViewController : UITableViewController
3677 @property(nonatomic,strong)NSArray *allNews;
3678 @end
3679 NewsViewController.m
3680 #import "NewsViewController.h"
3681 #import "News.h"
3682 #import "MyCell.h"
3683 
3684 @interface NewsViewController ()<UIScrollViewDelegate>
3685 
3686 @property(nonatomic,strong) UIScrollView *scrollView;
3687 @property(nonatomic,strong) NSArray *labels;;
3688 @end
3689 
3690 @implementation NewsViewController
3691 
3692 static NSString *cellIdentifier = @"cell";
3693 
3694 -(NSArray*)allNews{
3695     if (!_allNews) {
3696         _allNews=[News demoData];
3697     }
3698     return _allNews;
3699 }
3700 
3701 
3702 - (void)viewDidLoad
3703 {
3704     [super viewDidLoad];
3705 
3706     [self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
3707     
3708     
3709     
3710     self.scrollView=[[UIScrollView alloc]init];
3711     self.scrollView.frame=CGRectMake(0, 0, self.view.frame.size.width, 30);
3712     
3713     self.scrollView.contentSize=CGSizeMake(610, 30);
3714     
3715      self.scrollView.showsHorizontalScrollIndicator=NO;
3716     
3717     self.scrollView.delegate=self;
3718     
3719     
3720     UILabel *titleLabel1=[[UILabel alloc]initWithFrame:CGRectMake(55, 00,100, 30)];
3721     titleLabel1.text=@"体育";
3722     UILabel *titleLabel2=[[UILabel alloc]initWithFrame:CGRectMake(155, 00, 100, 30)];
3723     titleLabel2.text=@"非新闻";
3724     UILabel *titleLabel3=[[UILabel alloc]initWithFrame:CGRectMake(255, 00, 100, 30)];
3725     titleLabel3.text=@"科技";
3726     UILabel *titleLabel4=[[UILabel alloc]initWithFrame:CGRectMake(355, 00, 100, 30)];
3727     titleLabel4.text=@"军事";
3728     UILabel *titleLabel5=[[UILabel alloc]initWithFrame:CGRectMake(455, 00, 100, 30)];
3729     titleLabel5.text=@"历史";
3730     
3731     self.labels=@[titleLabel1,titleLabel2,titleLabel3,titleLabel4,titleLabel5];
3732     
3733     
3734     for (UILabel *label in self.labels) {
3735         label.textAlignment=NSTextAlignmentCenter;
3736         [self.scrollView addSubview:label];
3737     }
3738     
3739     
3740     self.scrollView.bounces=NO;
3741     
3742     self.navigationItem.titleView=self.scrollView;
3743     
3744     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
3745     self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil];
3746     
3747     
3748     
3749     UIView *tableHeader=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
3750     
3751     
3752     UIImageView *headerView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
3753     headerView.image=[UIImage imageNamed:@"header.png"];
3754     
3755     [tableHeader addSubview:headerView];
3756     
3757     self.tableView.tableHeaderView=tableHeader;
3758     
3759     self.tableView.showsVerticalScrollIndicator=NO;
3760 }
3761 
3762 
3763 #pragma mark - Table view data source
3764 
3765 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3766 {
3767     return 1;
3768 }
3769 
3770 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3771 {
3772 
3773     return self.allNews.count;
3774 }
3775 
3776 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3777 {
3778     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
3779     News *news = self.allNews[indexPath.row];
3780     
3781     cell.titleLabel.text=news.title;
3782     cell.newsImageView.image = [UIImage imageNamed:news.newsImageName];
3783     cell.commentNumberLabel.text = [NSString stringWithFormat:@"%d",news.commentNumber];
3784     
3785     
3786     return cell;
3787 }
3788 
3789 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
3790     return 100;
3791     
3792 }
3793 
3794 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
3795     CGPoint offSet=self.scrollView.contentOffset;
3796     
3797     for (int i=0; i<self.labels.count; i++) {
3798         if (i==(int)offSet.x/80) {
3799             UILabel* label=self.labels[i];
3800             [label setTextColor:[UIColor redColor]];
3801         } else {
3802             UILabel* label=self.labels[i];
3803             [label setTextColor:[UIColor blackColor]];
3804         }
3805     }
3806     
3807     [self.navigationItem.titleView setNeedsDisplay];
3808     
3809     
3810 }
3811 
3812 作业:
3813 1.照片查看器
3814             在一个TableView中列出所有照片的缩小图、照片名称和拍摄的位置。当用户点击某一行时,推出一个vc,在此vc中显示大的图片,大的图片允许缩放。
3815 Pictures.h
3816 #import <Foundation/Foundation.h>
3817 
3818 @interface Pictures : NSObject
3819 @property(nonatomic,strong)NSString *name;
3820 -(instancetype)initWithName:(NSString *)name;
3821 
3822 +(NSArray *)DemoData;
3823 @end
3824 Pictures.m
3825 #import "Pictures.h"
3826 
3827 @implementation Pictures
3828 -(instancetype)initWithName:(NSString *)name
3829 {
3830     if (self = [super init]) {
3831         self.name = name;
3832     }
3833     return self;
3834 }
3835 +(NSArray *)DemoData
3836 {
3837     Pictures *p1 = [[Pictures alloc]initWithName:@"welcome1.PNG"];
3838     Pictures *p2 = [[Pictures alloc]initWithName:@"welcome2.PNG"];
3839     Pictures *p3 = [[Pictures alloc]initWithName:@"welcome3.PNG"];
3840     Pictures *p4 = [[Pictures alloc]initWithName:@"welcome4.PNG"];
3841     return @[p1,p2,p3,p4];
3842     
3843     
3844 }
3845 @end
3846 MyTableViewController.h
3847 MyTableViewController.m
3848 #import "MyTableViewController.h"
3849 #import "DetailViewController.h"
3850 @interface MyTableViewController ()
3851 @property(nonatomic,strong)NSArray *pictures;
3852 
3853 @end
3854 
3855 @implementation MyTableViewController
3856 
3857 -(NSArray *)pictures
3858 {
3859     if (!_pictures) {
3860         _pictures = [Pictures DemoData];
3861         
3862     }
3863     return _pictures;
3864 }
3865 
3866 - (void)viewDidLoad
3867 {
3868     [super viewDidLoad];
3869    }
3870 
3871 
3872 #pragma mark - Table view data source
3873 
3874 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
3875 {
3876     return 1;
3877 }
3878 
3879 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3880 {
3881     return self.pictures.count;
3882 }
3883 
3884 
3885 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3886 {
3887     UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
3888     
3889     cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
3890     if (cell == nil) {
3891         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
3892         
3893     }
3894     Pictures *picture = self.pictures[indexPath.row];
3895     
3896     if (indexPath.row == 0) {
3897         cell.textLabel.text =picture.name;
3898         cell.detailTextLabel.text = @"achao";
3899         cell.imageView.image = [UIImage imageNamed:picture.name];
3900     }else if (indexPath.row == 1)
3901     {
3902         cell.textLabel.text =picture.name;
3903         cell.detailTextLabel.text = @"achao";
3904         cell.imageView.image = [UIImage imageNamed:picture.name];
3905         
3906     }else if (indexPath.row == 2)
3907     {
3908         cell.textLabel.text =picture.name;
3909         cell.detailTextLabel.text = @"achao";
3910         cell.imageView.image = [UIImage imageNamed:picture.name];
3911         
3912     }else if (indexPath.row == 3)
3913     {
3914         cell.textLabel.text =picture.name;
3915         cell.detailTextLabel.text = @"achao";
3916         cell.imageView.image = [UIImage imageNamed:picture.name];
3917         
3918     }
3919 
3920     return cell;
3921 }
3922 
3923 
3924 #pragma mark - Table view delegate
3925 
3926 // In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
3927 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
3928 {
3929     
3930     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
3931     
3932     // Pass the selected object to the new view controller.
3933     Pictures *picture = self.pictures[indexPath.row];
3934     detailViewController.picture = picture;
3935     // Push the view controller.
3936     [self.navigationController pushViewController:detailViewController animated:YES];
3937 }
3938 @end
3939 DetailViewController.h
3940 #import <UIKit/UIKit.h>
3941 #import "Pictures.h"
3942 @interface DetailViewController : UIViewController
3943 @property(nonatomic,strong)Pictures *picture;
3944 
3945 @end
3946 DetailViewController.m
3947 #import "DetailViewController.h"
3948 #import "Pictures.h"
3949 @interface DetailViewController ()<UIScrollViewDelegate>
3950 
3951 @property(nonatomic,strong)UIScrollView *scrollView;
3952 @property(nonatomic,strong)UIImageView *imageView;
3953 
3954 @end
3955 
3956 @implementation DetailViewController
3957 
3958 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
3959 {
3960     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
3961     if (self) {
3962         // Custom initialization
3963     }
3964     return self;
3965 }
3966 
3967 - (void)viewDidLoad
3968 {
3969     [super viewDidLoad];
3970 }
3971 
3972 -(void)viewWillAppear:(BOOL)animated
3973 {
3974     [super viewWillAppear:animated];
3975     NSString *name = self.picture.name;
3976     NSLog(@"->%@",name);
3977     UIImage *image= [UIImage imageNamed:name];
3978     UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
3979     //imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
3980     imageView.contentMode = UIViewContentModeScaleAspectFit;
3981     self.imageView = imageView;
3982     
3983     UIScrollView *scrollView = [[UIScrollView alloc]init];
3984     self.scrollView = scrollView;
3985     scrollView.contentSize = imageView.frame.size;
3986     scrollView.contentMode = UIViewContentModeScaleAspectFit;
3987     scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
3988     scrollView.maximumZoomScale = 1;
3989     CGFloat xScale = scrollView.frame.size.width/imageView.frame.size.width;
3990     CGFloat yScale = scrollView.frame.size.height/imageView.frame.size.height;
3991     scrollView.minimumZoomScale = MIN(xScale, yScale);
3992     
3993     scrollView.delegate = self;
3994     [scrollView addSubview:imageView];
3995     [self.view addSubview:scrollView];
3996 }
3997 
3998 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
3999 {
4000     return self.imageView;
4001 }
4002 AppDelegate.h
4003 AppDelegate.m
4004 #import "AppDelegate.h"
4005 #import "MyTableViewController.h"
4006 @implementation AppDelegate
4007 
4008 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4009 {
4010     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
4011     MyTableViewController *myVC = [[MyTableViewController alloc]initWithNibName:@"MyTableViewController" bundle:nil];
4012     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:myVC];
4013     self.window.rootViewController = navi;
4014     [self.window makeKeyAndVisible];
4015     return YES;
4016 }
4017 
4018 1.静态表格
4019     行数不变的表格
4020         
4021         实现方式:
4022         方法一:代码方式
4023 
4024                 特点:使用大量的if  else  进行界面的硬编码
4025                 注意:静态表格中的数据依然是可变的
4026 User.h                
4027 #import <Foundation/Foundation.h>
4028 
4029 @interface User : NSObject
4030 
4031 @property(nonatomic,strong)NSString *weChatNumber;
4032 @property(nonatomic,strong)NSString *qqNumber;
4033 @property(nonatomic,strong)NSString *phoneNumber;
4034 @property(nonatomic,strong)NSString *email;
4035 
4036 @end
4037 User.m
4038 4039 SafeViewController.h
4040 SafeViewController.m
4041 #import "SafeViewController.h"
4042 
4043 @interface SafeViewController ()
4044 
4045 @end
4046 
4047 @implementation SafeViewController
4048 
4049 - (void)viewDidLoad
4050 {
4051     [super viewDidLoad];
4052     self.title = @"账号与安全";
4053     //设置导航栏的背景色
4054     self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
4055 }
4056 
4057 #pragma mark - Table view data source
4058 
4059 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
4060 {
4061     return 2;
4062 }
4063 
4064 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
4065 {
4066     if (section == 0) {
4067         return 1;
4068     }else{
4069         return 3;
4070     }
4071 }
4072 
4073 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4074 {
4075     UITableViewCell *cell = nil;
4076     switch (indexPath.section) {
4077         case 0://分区0
4078             cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
4079             cell.textLabel.text = @"微信号";
4080             cell.detailTextLabel.text = self.user.weChatNumber;
4081             break;
4082         case 1://分区1
4083             if (indexPath.row == 0) {
4084                 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
4085                 cell.textLabel.text = @"QQ号";
4086                 cell.detailTextLabel.text = self.user.qqNumber;
4087                 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
4088             }else if(indexPath.row == 1){
4089                 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
4090                 cell.textLabel.text = @"手机号";
4091                 cell.detailTextLabel.text = self.user.phoneNumber;
4092                 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
4093                 
4094             }else{
4095                 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
4096                 cell.textLabel.text = @"邮箱";
4097                 cell.detailTextLabel.text = self.user.email;
4098                 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
4099             }
4100             break;
4101         default:
4102             break;
4103     }
4104     
4105     return cell;
4106 }
4107 AppDelegate.h
4108 AppDelegate.m
4109 #import "AppDelegate.h"
4110 #import "SafeViewController.h"
4111 #import "User.h"
4112 
4113 @implementation AppDelegate
4114 
4115 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4116 {
4117     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
4118     User *user = [[User alloc]init];
4119     user.weChatNumber = @"chat-num";
4120     user.qqNumber = @"1234567";
4121     user.phoneNumber = @"18600xxxx";
4122     user.email = @"tom@tarena.com.cn";
4123     SafeViewController *vc = [[SafeViewController alloc]initWithNibName:@"SafeViewController" bundle:nil];
4124     vc.user = user;
4125     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
4126     self.window.rootViewController = navi;
4127     [self.window makeKeyAndVisible];
4128     return YES;
4129 }
4130 结果:
4131 4132 
4133         方法二:xib方法
4134     
4135                 实现思路:在xib中将静态表格中的每一行都通过拖拽一个对象来进行设计,系统会自动创建xib中包含的所有的控件对象,但是,默认每一个xib文件只能有一个根元素被控制器加载,其他拖拽后并由系统创建了的对象,可以通过将这个对象连线到控制器中成为属性再访问。
4136              【Demo2_StaticTable_Xib】
4137 
4138 SettingViewController.h                    
4139 SettingViewController.m
4140 #import "SettingViewController.h"
4141 @interface SettingViewController ()
4142 4143 @property (strong, nonatomic) IBOutlet UIView *headerView;  连线
4144 @property (strong, nonatomic) IBOutlet UITableViewCell *greenDimand;
4145 @property (strong, nonatomic) IBOutlet UITableViewCell *freeMode;
4146 @property (strong, nonatomic) IBOutlet UITableViewCell *settingClose;
4147 @property (strong, nonatomic) IBOutlet UITableViewCell *exit;
4148 
4149 @end
4150 
4151 @implementation SettingViewController
4152 
4153 - (void)viewDidLoad
4154 {
4155     [super viewDidLoad];
4156     self.title = @"更多";
4157     self.tableView.tableHeaderView = self.headerView;
4158 }
4159 
4160 #pragma mark - Table view data source
4161 
4162 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
4163 {
4164     return 3;
4165 }
4166 
4167 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
4168 {
4169     switch (section) {
4170         case 0:
4171             return 2;
4172         case 1:
4173             return 1;
4174         case 2:
4175             return 1;
4176         default:
4177             return 0;
4178     }
4179 }
4180 
4181 
4182 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4183 {
4184     UITableViewCell *cell = nil;
4185     switch (indexPath.section) {
4186         case 0:
4187             if (indexPath.row == 0 ) {
4188                 //根据分区号和行号,cell指向不同的对象,然后返回
4189                 cell = self.greenDimand;
4190             }else{
4191                 cell = self.freeMode;
4192             }
4193             break;
4194         case 1:
4195             cell = self.settingClose;
4196             break;
4197         case 2:
4198             cell = self.exit;
4199             break;
4200     }
4201     return cell;
4202 }
4203 
4204 2.动态表格
4205 数据行是不固定的
4206 核心理念:创建完TableView之后,设置tableView的dataSource和delegate对象,只要符合协议的对象都可以设置为代理方法
4207                 选择一:让当前控制器遵守协议,然后成为tableView的代理
4208                 选择二:自己编写类,遵守协议,然后创建类的对象,设置为tableView的代理
4209 方法一:
4210 tableView.dataSource和tableView.delegate两个代理交给两个类处理,不用当前控制器处理。两个类需遵守协议UITableViewDataSource和UITableViewDelegate,当前控制器需创建两个类的实例化,才能将其成为代理方
4211 ViewController.xib
4212 添加Table View控件,修改大小,连线代理,
4213 ViewController.h 控制器
4214 #import <UIKit/UIKit.h>
4215 
4216 @interface ViewController : UIViewController
4217 
4218 @end
4219 ViewController.m
4220 #import "ViewController.h"
4221 #import "TRTableDateSource.h"
4222 #import "TRTableDalegate.h"
4223 
4224 @interface ViewController ()
4225 @property(nonatomic,strong)TRTableDateSource* tableDateSource;
4226 @property(nonatomic,strong)TRTableDalegate* tableDalegate;
4227 @end
4228 
4229 @implementation ViewController
4230 //重写get方法
4231 -(TRTableDateSource *)tableDateSource{
4232     if (!_tableDateSource) {
4233         _tableDateSource=[[TRTableDateSource alloc]init];
4234     }
4235     return _tableDateSource;
4236 }
4237 
4238 -(TRTableDalegate *)tableDalegate{
4239     if (!_tableDalegate) {
4240         _tableDalegate=[[TRTableDalegate alloc]init];
4241     }
4242     return _tableDalegate;
4243 }
4244 
4245 //UITableView继承UIScrollView
4246 - (void)viewDidLoad
4247 {
4248     [super viewDidLoad];
4249     UITableView* tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
4250     tableView.dataSource=self.tableDateSource;
4251     tableView.delegate=self.tableDalegate;
4252     [self.view addSubview:tableView];
4253 }
4254 
4255 @end
4256 TRTableDateSource.h
4257 #import <Foundation/Foundation.h>
4258 
4259 @interface TRTableDateSource : NSObject<UITableViewDataSource>//遵守协议
4260 
4261 @end
4262 
4263 TRTableDateSource.m
4264 #import "TRTableDateSource.h" //数据源
4265 
4266 @implementation TRTableDateSource
4267 
4268 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
4269     return 1;
4270 }
4271 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
4272     return 10;
4273 }
4274 
4275 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
4276     UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"cell" ];
4277     if (cell==nil) {
4278         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
4279         
4280     }
4281     cell.textLabel.text=@"hello";
4282     return cell;
4283 }
4284 @end
4285 TRTableDalegate.h
4286 #import <Foundation/Foundation.h>
4287 
4288 @interface TRTableDalegate : NSObject<UITableViewDelegate>//遵守协议
4289 
4290 @end
4291 TRTableDalegate.m
4292 #import "TRTableDalegate.h"//代理响应
4293 
4294 @implementation TRTableDalegate
4295 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
4296     NSLog(@"...");
4297 }
4298 
4299 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
4300     return 100;
4301 }
4302 @end
4303 AppDelegate.h
4304 AppDelegate.m
4305 #import "AppDelegate.h"
4306 #import "ViewController.h"
4307 
4308 @implementation AppDelegate
4309 
4310 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4311 {
4312     self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
4313     ViewController* cV=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
4314     self.window.rootViewController=cV;
4315     [self.window makeKeyAndVisible];
4316     return YES;
4317 }
4318 方法2:
4319    需求:在一个普通vc的视图上有两个tableView,上面的tableView从资源库中拖拽,用于显示5行Hello World;下面的tableView使用代码创建,用于显示7行Hello Kitty;当前控制器对象同时是这两个tableView的dataSource和delegate
4320 TRViewController.h
4321 TRViewController.m
4322 #import "TRViewController.h"
4323 
4324 @interface TRViewController ()<UITableViewDataSource,UITableViewDelegate>
4325 @property(nonatomic,strong)UITableView* codeTableView;//手写代码
4326 
4327 @property (weak, nonatomic) IBOutlet UITableView *xibTableView;//通过xib创建,就可以省了viewDidLoad中的4步
4328 @end
4329 
4330 @implementation TRViewController
4331 
4332 - (void)viewDidLoad
4333 {
4334     [super viewDidLoad];
4335     self.codeTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 220, self.view.frame.size.width, 250)];
4336     self.codeTableView.dataSource=self;
4337     self.codeTableView.delegate=self;
4338     [self.view addSubview:self.codeTableView];
4339 }
4340 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
4341     return 1;
4342 }
4343 
4344 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
4345     if (tableView==self.xibTableView) {
4346         return 5;
4347     }else{
4348         return 7;
4349     }
4350 }
4351 
4352 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
4353     UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
4354     if (cell==nil) {
4355         if (tableView==self.xibTableView) {
4356             cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
4357         }else{
4358             cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
4359         }
4360         
4361     }
4362     
4363     if (tableView==self.xibTableView) {
4364         cell.textLabel.text=@"HelloWord";
4365     }else{
4366         cell.textLabel.text=@"HelloKitty";
4367         cell.detailTextLabel.text=@"miao";
4368     }
4369     return cell;
4370 }
4371 @end
4372 结果:
4373 4374 练习:
4375 展示地区名称,层级未知,取决于数据模型
4376 北京
4377         东城
4378         西城
4379         朝阳
4380             潘家园
4381             松榆路
4382                 建业苑
4383                         一层
4384                         六层
4385                             达内
4386                                     第一教室
4387                                     第二教室
4388                             达内对面
4389             麦当劳
4390         海淀
4391 上海
4392         徐汇
4393         静安
4394 
4395 +AreaTableViewController
4396         +areas: Area( 元素类型:Area)
4397 
4398 
4399 +Area
4400         +name:NSString
4401         +subAreas:NSArray
4402                 +[Item]:Area
4403                         +name:NSString
4404                         +subAreas:NSArray
4405                                 +[Item]:Arear
4406                                 ……
4407 Area.h
4408 #import <Foundation/Foundation.h>
4409 
4410 @interface Area : NSObject
4411 
4412 @property(nonatomic,strong)NSString *name;
4413 @property(nonatomic,strong)NSArray *subAreas;
4414 
4415 -(instancetype)initWithName:(NSString *)name;
4416 
4417 +(Area *)demoData;
4418 
4419 @end
4420 Area.m
4421 #import "Area.h"
4422 
4423 @implementation Area
4424 
4425 - (instancetype)initWithName:(NSString *)name{
4426     self = [super init];
4427     if (self) {
4428         self.name = name;
4429     }
4430     return self;
4431 }
4432 
4433 + (Area *)demoData{
4434     Area *a0 = [[Area alloc]initWithName:@"城市"];
4435     Area *a1 = [[Area alloc]initWithName:@"北京"];
4436     Area *a2 = [[Area alloc]initWithName:@"上海"];
4437     Area *a11 = [[Area alloc]initWithName:@"东城"];
4438     Area *a12 = [[Area alloc]initWithName:@"西城"];
4439     Area *a13 = [[Area alloc]initWithName:@"朝阳"];
4440     Area *a14 = [[Area alloc]initWithName:@"徐汇"];
4441     Area *a15 = [[Area alloc]initWithName:@"静安"];
4442     Area *a21 = [[Area alloc]initWithName:@"潘家园"];
4443     Area *a22 = [[Area alloc]initWithName:@"松榆路"];
4444     Area *a23 = [[Area alloc]initWithName:@"麦当劳"];
4445     Area *a31 = [[Area alloc]initWithName:@"建业苑"];
4446     Area *a41 = [[Area alloc]initWithName:@"一楼"];
4447     Area *a42 = [[Area alloc]initWithName:@"六楼"];
4448     Area *a51 = [[Area alloc]initWithName:@"达内"];
4449     Area *a61 = [[Area alloc]initWithName:@"第一教室"];
4450     Area *a62= [[Area alloc]initWithName:@"第二教室"];
4451                   
4452     a51.subAreas = @[a61,a62];
4453     a42.subAreas = @[a51];
4454     a31.subAreas=@[a41,a42];
4455     a22.subAreas = @[a31];
4456     a13.subAreas = @[a21,a22,a23];
4457     a1.subAreas = @[a11,a12,a13];
4458     a2.subAreas = @[a14,a15];
4459     a0.subAreas = @[a1,a2];
4460     
4461     return a0;
4462 }
4463 
4464 @end
4465 AreaTableViewController.h
4466 #import <UIKit/UIKit.h>
4467 #import "Area.h"
4468 
4469 @interface AreaTableViewController : UITableViewController
4470 //每一个控制器实例对应的界面都是用来显示
4471 //一个area的实例
4472 //将area显示到表视图中
4473 @property(nonatomic,strong)Area *area;
4474 @end
4475 AreaTableViewController.m
4476 #import "AreaTableViewController.h"
4477 
4478 @interface AreaTableViewController ()
4479 
4480 @end
4481 
4482 @implementation AreaTableViewController
4483 
4484 - (void)viewDidLoad
4485 {
4486     [super viewDidLoad];
4487     //1.area实例的name放在导航栏的标题位置
4488     self.title = self.area.name;
4489 }
4490 
4491 /*
4492  表格显示的是:area中的所有子地区对象的名字
4493  */
4494 #pragma mark - Table view data source
4495 
4496 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
4497 {
4498     return 1;
4499 }
4500 
4501 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
4502 {
4503     return self.area.subAreas.count;
4504 }
4505 
4506 
4507 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4508 {
4509     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
4510     if (cell == nil) {
4511         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
4512     }
4513     //每一个单元格用于显示area的一个子地区信息
4514     Area *subArea = self.area.subAreas[indexPath.row];
4515     cell.textLabel.text = subArea.name;
4516     if (subArea.subAreas!=nil) {
4517         //如果有子地区,显示大于号做提示
4518         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
4519     }else{
4520         //如果没有子地区,修改行的默认选中样式
4521         //在选中后不出现灰色的背景
4522         cell.selectionStyle = UITableViewCellSelectionStyleNone;
4523     }
4524     return cell;
4525 }
4526 
4527 //点击某一行之后推出新的界面,推出的界面与当前界面显示样式相同的,只是数据不同
4528 //而已,选中一个节点,其实代表的选中依然是一个Area的实例,所以在推出新的界面时,将选中的Area实例
4529 //作为参数传过去,由推出的界面负责显示
4530 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
4531     //获取选中的节点对应的Area实例
4532     Area *subArea = self.area.subAreas[indexPath.row];
4533     if (subArea.subAreas!=nil) {
4534         //创建新的控制器实例
4535         AreaTableViewController *tVC = [[AreaTableViewController alloc]initWithNibName:@"AreaTableViewController" bundle:nil];
4536         tVC.area = subArea;
4537         //推出新的控制器
4538         [self.navigationController pushViewController:tVC animated:YES];
4539     }
4540    
4541 }
4542 
4543 
4544 @end
4545 AppDelegate.h
4546 AppDelegate.m
4547 #import "AppDelegate.h"
4548 #import "Area.h"
4549 #import "AreaTableViewController.h"
4550 
4551 @implementation AppDelegate
4552 
4553 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4554 {
4555     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
4556     AreaTableViewController *tvc = [[AreaTableViewController alloc]initWithNibName:@"AreaTableViewController" bundle:nil];
4557     Area *area = [Area demoData];
4558     tvc.area = area;
4559     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:tvc];
4560     self.window.rootViewController = navi;
4561     [self.window makeKeyAndVisible];
4562     
4563     return YES;
4564 }
4565 ========================================================================================================================
4566 知识点
4567 十三、UITabBarController标签控制器
4568 
4569   1.是什么?
4570             与navigationController很像,也是管理控制器的控制器。
4571             tabBarController没有完整的界面外观,靠管理其它的vc来完成界面的显示。
4572             管理方式横向并列方式的,navigation倾向于垂直深入式的管理vc。
4573 
4574  1.如何使用?
4575             step1:将tabBarController管理的多个vc实例化出具体的对象
4576             step2:将多个vc存到tabBar控制器中
4577        1.1创建
4578 #import "AppDelegate.h"
4579 #import "AViewController.h"
4580 #import "BViewController.h"
4581 #import "CViewController.h"
4582 
4583 @implementation AppDelegate
4584 
4585 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4586 {
4587     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
4588     AViewController *avc = [[AViewController alloc]initWithNibName:@"AViewController" bundle:nil];
4589     BViewController *bvc = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
4590     CViewController *cvc = [[CViewController alloc]initWithNibName:@"CViewController" bundle:nil];
4591     
4592     // 创建tabBar的实例
4593     UITabBarController *tabVC = [[UITabBarController alloc]init];
4594     //设置颜色
4595     tabVC.tabBar.tintColor = [UIColor redColor];
4596     //设置tabBar的代理
4597     tabVC.delegate = self;
4598     // 将多个vc放到tabBar中
4599     tabVC.viewControllers = @[avc,bvc,cvc];
4600     self.window.rootViewController = tabVC;
4601     [self.window makeKeyAndVisible];
4602     return YES;
4603 }
4604      1.2设置
4605         - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4606 {
4607     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
4608     if (self) {
4609         //配置文字
4610         self.title = @"AVC";
4611         //self.tabBarItem.title = @"AVC";
4612         //配置图片
4613         self.tabBarItem.image =[UIImage imageNamed:@"line_bell"];
4614         self.tabBarItem.selectedImage = [UIImage imageNamed:@"full_bell"];
4615         self.tabBarItem.badgeValue = @"2";//配置徽章
4616     }
4617     return self;
4618 }
4619 
4620 
4621  1. tabBar的配置
4622             .tabBarItem.title    文字
4623             .tabBarItem.image     不点击时是     空心图片
4624             .tabBarItem.selectedImage   点击的时候是    实心图片
4625             .tabBarItem.badgeValue       徽章
4626 
4627             tabBarController.tabBar.tintColor   颜色
4628 
4629             tabBarController.selectedIndex 可读可写的属性,用于获取或设置tabBar中激活的vc的下标
4630 
4631             2.响应用户选中某vc事件:
4632             - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
4633 
4634 4.tabBarController与navigationController的配合
4635         原则:
4636             a。多引导性控制器共存时,tabBar是整体最外层的管理者,navigation被tabBar管理的一个分支控制器
4637             b。显示第一屏时,tabBar可以看见,在某一个具体 的vc中推出新vc时,tabBar处于隐藏状态。只有回到顶级vc时,tabBar区域才可见
4638             c。推出新vc时,隐藏底部各种bar的方法:
4639                 vc.hidesBottomBarWhenPushed = YES;
4640 
4641 例:
4642 AppDelegate.h
4643 #import <UIKit/UIKit.h>
4644 
4645 @interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate> //遵守协议
4646 
4647 @property (strong, nonatomic) UIWindow *window;
4648 
4649 @end
4650 AppDelegate.m
4651 #import "AppDelegate.h"
4652 #import "AViewController.h"
4653 #import "BViewController.h"
4654 #import "CViewController.h"
4655 
4656 @implementation AppDelegate
4657 
4658 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
4659     
4660     NSInteger index = tabBarController.selectedIndex;
4661     NSLog(@"%d",index);
4662     //选中tab中的某一个项后触发
4663     //viewController.tabBarItem.badgeValue = nil;
4664 }
4665 
4666 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
4667 {
4668     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
4669     AViewController *avc = [[AViewController alloc]initWithNibName:@"AViewController" bundle:nil];
4670     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:avc];
4671     
4672     BViewController *bvc = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];
4673     CViewController *cvc = [[CViewController alloc]initWithNibName:@"CViewController" bundle:nil];
4674     // 创建tabBar的实例
4675     UITabBarController *tabVC = [[UITabBarController alloc]init];
4676     //设置颜色
4677     tabVC.tabBar.tintColor = [UIColor redColor];
4678     //设置tabBar的代理
4679     tabVC.delegate = self;
4680     // 将多个vc放到tabBar中
4681     tabVC.viewControllers = @[navi,bvc,cvc];
4682     self.window.rootViewController = tabVC;
4683     [self.window makeKeyAndVisible];
4684     return YES;
4685 }
4686 AViewController.h
4687 AViewController.m
4688 #import "AViewController.h"
4689 #import "OtherViewController.h"
4690 
4691 @interface AViewController ()
4692 
4693 @end
4694 
4695 @implementation AViewController
4696 
4697 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4698 {
4699     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
4700     if (self) {
4701         //配置文字
4702         self.title = @"AVC";
4703         //self.tabBarItem.title = @"AVC";
4704         //配置图片
4705         self.tabBarItem.image =[UIImage imageNamed:@"line_bell"];
4706         self.tabBarItem.selectedImage = [UIImage imageNamed:@"full_bell"];
4707         self.tabBarItem.badgeValue = @"2";//配置徽章
4708     }
4709     return self;
4710 }
4711 
4712 - (void)viewDidLoad
4713 {
4714     [super viewDidLoad];
4715     //配置导航的右侧按钮
4716     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(gotoOther:)];
4717 }
4718 
4719 -(void)gotoOther:(UIBarButtonItem *)barButton{
4720     //推出otherVC
4721     OtherViewController *ovc = [[OtherViewController alloc]initWithNibName:@"OtherViewController" bundle:nil];
4722     //推出时,隐藏底部区域
4723     ovc.hidesBottomBarWhenPushed = YES;
4724     
4725     [self.navigationController pushViewController:ovc animated:YES];
4726 }
4727 
4728 
4729 //点击按钮,跳到第三个界面
4730 - (IBAction)gotoCVC:(id)sender {
4731     self.tabBarController.selectedIndex = 2;  连线按钮
4732 }
4733 
4734 @end
4735 
4736 想要哪个界面显示,就配置哪个界面
4737 BViewController.h
4738 BViewController.m
4739 
4740 CViewController.h
4741 CViewController.m
4742 
4743 OtherViewController.h
4744 OtherViewController.m
4745 4746 知识点
4747 十四、其它控件    
4748 
4749 1. SegmentedControl 分段控件
4750   SegmentedControl 分段控件
4751             属性:
4752                     selectedSegmentIndex  选中的分段按钮的下标
4753             事件:
4754                     valueChanged事件
4755 SegViewController.h
4756 SegViewController.m
4757 #import "SegViewController.h"
4758 
4759 @interface SegViewController ()
4760 
4761 @property(nonatomic,strong)NSArray *headerImageNames;
4762 
4763 @property (weak, nonatomic) IBOutlet UIImageView *headerImageView; 连线属性如下图
4764 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmented;
4765 
4766 @end
4767 
4768 @implementation SegViewController
4769 
4770 - (NSArray *)headerImageNames{
4771     if (!_headerImageNames) {
4772         _headerImageNames =@[@"Brad Cox.png",@"Dennis Ritchie.png",@"Ray.png"];
4773     }
4774     return _headerImageNames;
4775 }
4776 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4777 {
4778     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
4779     if (self) {
4780         self.title = @"分段";
4781         self.tabBarItem.image = [UIImage imageNamed:@"line_ball"];
4782     }
4783     return self;
4784 }
4785 
4786 - (void)viewDidLoad
4787 {
4788     [super viewDidLoad];
4789     self.navigationItem.titleView = self.segmented;
4790 }
4791 
4792 - (void)viewWillAppear:(BOOL)animated{
4793     [super viewWillAppear:animated];
4794     [self changeHeaderImage:self.segmented];
4795 }
4796 
4797 - (IBAction)changeHeaderImage:(UISegmentedControl *)sender {
4798     //选择的segmented中的按钮的索引
4799     NSInteger index = sender.selectedSegmentIndex;
4800     self.headerImageView.image = [UIImage imageNamed:self.headerImageNames[index]];
4801 }
4802 
4803 @end
4804 
4805 
4806 2. Activity Indicator 活动指示器
4807 IndicatorViewController.h
4808 IndicatorViewController.m
4809 #import "IndicatorViewController.h"
4810 
4811 @interface IndicatorViewController ()
4812 @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
4813 @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
4814 
4815 @end
4816 
4817 @implementation IndicatorViewController
4818 
4819 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4820 {
4821     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
4822     if (self) {
4823         self.title=@"指示器";
4824         self.tabBarItem.image = [UIImage imageNamed:@"line_cart"];
4825     }
4826     return self;
4827 }
4828 
4829 - (void)viewDidLoad
4830 {
4831     [super viewDidLoad];
4832     self.progressView.progress = 0;
4833 }
4834 
4835 - (IBAction)changeIndicator:(UIButton *)sender {
4836     //根据指示器的状态来决定是转动还是停止转动
4837     if (self.indicator.isAnimating) {
4838         [self.indicator stopAnimating];
4839     }else{
4840         [self.indicator startAnimating];
4841     }
4842 }
4843 - (IBAction)beginDownload:(id)sender {
4844     //启动一个计时器,每隔一段时间自动的执行动作
4845     //使用sheduledTime。。。方法创建的计时器
4846     //不需要启动已经开始工作
4847     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeProgress:) userInfo:nil repeats:YES];
4848     
4849 }
4850 
4851 -(void)changeProgress:(NSTimer *)timer{
4852     //修改进度条的进度
4853     self.progressView.progress+=0.1;
4854 
4855     if (self.progressView.progress >= 0.5) {  范围判断比较好
4856         // 停止计时器
4857         [timer invalidate];
4858     }
4859     }
4860 
4861 @end
4862 
4863 4864 3. Progress View 进度条
4865 
4866 4. Date Picker
4867 DateViewController.h
4868 DateViewController.m
4869 #import "DateViewController.h"
4870 
4871 @interface DateViewController ()
4872 @property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;连线
4873 @property (weak, nonatomic) IBOutlet UILabel *dateLabel;
4874 
4875 @end
4876 
4877 @implementation DateViewController
4878 
4879 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4880 {
4881     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
4882     if (self) {
4883         self.title = @"时间";
4884         self.tabBarItem.image = [UIImage imageNamed:@"line_paint"];
4885     }
4886     return self;
4887 }
4888 
4889 - (void)viewDidLoad
4890 {
4891     [super viewDidLoad];
4892     // Do any additional setup after loading the view from its nib.
4893 }
4894 
4895 - (IBAction)showDate:(UIButton *)sender {
4896     NSDate *date = self.datePicker.date;
4897     NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
4898     formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
4899     NSString *dateString = [formatter stringFromDate:date];
4900     self.dateLabel.text = dateString;
4901 }
4902 
4903 @end
4904     
4905 5. Picker View
4906 重要属性:
4907             date  获取选中后的时间对象
4908                 一般需要转换成指定的格式:
4909             NSDate *date = self.datePicker.date;
4910         NSDateFormatter *formatter =   [[NSDateFormatter alloc]init];
4911         formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
4912         NSString *dateString = [formatter stringFromDate:date];
4913 
4914 PickerViewController.h
4915 PickerViewController.m
4916 #import "PickerViewController.h"
4917 
4918 @interface PickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
4919 @property(nonatomic,strong)NSArray *fromCity;
4920 @property(nonatomic,strong)NSArray *toCity;
4921 
4922 @end
4923 
4924 @implementation PickerViewController
4925 
4926 - (NSArray *)fromCity{
4927     if (!_fromCity) {
4928         _fromCity = @[@"北京",@"上海",@"广州",@"深圳"];
4929     }
4930     return _fromCity;
4931 }
4932 
4933 - (NSArray *)toCity{
4934     if (!_toCity) {
4935         _toCity = @[@"杭州",@"苏州"];
4936     }
4937     return _toCity;
4938 }
4939 
4940 
4941 
4942 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4943 {
4944     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
4945     if (self) {
4946         self.title = @"picker view";
4947         self.tabBarItem.image = [UIImage imageNamed:@"line_map"];
4948     }
4949     return self;
4950 }
4951 
4952 - (void)viewDidLoad
4953 {
4954     [super viewDidLoad];
4955    
4956 }
4957 
4958 #pragma mark - UIPickerView DataSource
4959 //控件有几列
4960 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
4961     return 2;
4962 }
4963 
4964 //列中有几行
4965 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
4966     if (component == 0) {
4967         return self.fromCity.count;
4968     }else{
4969         return self.toCity.count;
4970     }
4971 }
4972 
4973 #pragma mark - UIPickerView Delegate
4974 
4975 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
4976     if (component == 0) {
4977         return self.fromCity[row];
4978     }else{
4979         return self.toCity[row];
4980     }
4981     
4982 }
4983 
4984 @end
4985 4986 
4987 
4988 RelationPickerViewController.h
4989 RelationPickerViewController.m
4990 #import "RelationPickerViewController.h"
4991 
4992 @interface RelationPickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
4993 @property(nonatomic,strong)NSDictionary *dictionaryCitys;
4994 @property(nonatomic,strong)NSArray *allCityName;
4995 @property(nonatomic,strong)NSArray *allAreaName;
4996 @end
4997 
4998 @implementation RelationPickerViewController
4999 
5000 - (NSDictionary *)dictionaryCitys{
5001     if (!_dictionaryCitys) {
5002         _dictionaryCitys = @{
5003            @"北京":@[@"东城",@"西城",@"海淀"],
5004            @"上海":@[@"徐汇",@"静安",@"浦东"],
5005            @"广州":@[@"白云",@"越秀",@"天河"]
5006         };
5007     }
5008     return _dictionaryCitys;
5009 }
5010 
5011 - (NSArray *)allCityName{
5012     if (!_allCityName) {
5013         _allCityName = self.dictionaryCitys.allKeys;
5014     }
5015     return _allCityName;
5016 }
5017 
5018 - (NSArray *)allAreaName{
5019     if (!_allAreaName) {
5020         _allAreaName = [self.dictionaryCitys objectForKey:self.allCityName[0]];
5021     }
5022     return _allAreaName;
5023 }
5024 
5025 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
5026 {
5027     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
5028     if (self) {
5029         self.title = @"地址";
5030         self.tabBarItem.image = [ UIImage imageNamed:@"line_umbrella"];
5031     }
5032     return self;
5033 }
5034 
5035 - (void)viewDidLoad
5036 {
5037     [super viewDidLoad];
5038 
5039 }
5040 
5041 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
5042     return 2;
5043 }
5044 
5045 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
5046     if (component==0) {
5047         return self.allCityName.count;
5048     }else{
5049         return self.allAreaName.count;
5050     }
5051 }
5052 
5053 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
5054     if (component==0) {
5055         return self.allCityName[row];
5056     }else{
5057         return self.allAreaName[row];
5058     }
5059 }
5060 
5061 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
5062     if (component == 0) {
5063         //1.获取第一列中选中的值
5064         NSString *cityName = self.allCityName[row];
5065         //2.根据第一列的值做key,找到对应的子地区
5066         self.allAreaName = [self.dictionaryCitys objectForKey:cityName];
5067         [pickerView reloadComponent:1];
5068         // 修改第二列第一行为选中的值
5069         [pickerView selectRow:0 inComponent:1 animated:YES];
5070     }
5071 }
5072 
5073 @end
5074 5075 总:
5076 AppDelegate.h
5077 AppDelegate.m
5078 #import "AppDelegate.h"
5079 #import "SegViewController.h"
5080 #import "IndicatorViewController.h"
5081 #import "DateViewController.h"
5082 #import "PickerViewController.h"
5083 #import "RelationPickerViewController.h"
5084 
5085 @implementation AppDelegate
5086 
5087 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
5088 {
5089     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
5090     SegViewController *segVC = [[SegViewController alloc]initWithNibName:@"SegViewController" bundle:nil];
5091     UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:segVC];
5092     
5093     IndicatorViewController *indicatorVC = [[IndicatorViewController alloc]initWithNibName:@"IndicatorViewController" bundle:nil];
5094     
5095     DateViewController *dateVC = [[DateViewController alloc]initWithNibName:@"DateViewController" bundle:nil];
5096     
5097     PickerViewController *pVC = [[PickerViewController alloc]initWithNibName:@"PickerViewController" bundle:nil];
5098     
5099     RelationPickerViewController *rVC = [[RelationPickerViewController alloc]initWithNibName:@"RelationPickerViewController" bundle:nil];
5100     
5101     
5102     UITabBarController *tabVC = [[UITabBarController alloc]init];
5103     tabVC.viewControllers = @[navi,indicatorVC,dateVC,pVC,rVC];
5104     self.window.rootViewController = tabVC;
5105     [self.window makeKeyAndVisible];
5106     return YES;
5107 }
5108 
5109 
5110 3.iOS8中的警告框和操作表
5111   3.1  创建
5112             UIAlertController *alertController =
5113         [UIAlertController alertControllerWithTitle:@"Title"message:@"messsage"preferredStyle:UIAlertControllerStyleAlert];
5114 
5115 
5116  3.2 添加动作
5117             UIAlertAction *cancelAction =        [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel  handler:^(UIAlertAction *action) { NSLog(@"Cancel");}];
5118             
5119             [alertController addAction:cancelAction];
5120 
5121  3.3 添加文本框
5122             [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {textField.secureTextEntry = YES;}];
5123 
5124  3.4 推出显示警告框
5125             [self presentViewController:alertController animated:YES completion:nil];
5126 
5127 例:ios6
5128 AlertViewController.h
5129 AlertViewController.m
5130 #import "AlertViewController.h"
5131 
5132 @interface AlertViewController ()
5133 
5134 @end
5135 
5136 @implementation AlertViewController
5137 
5138 - (void)viewDidLoad {
5139     [super viewDidLoad];
5140     // Do any additional setup after loading the view from its nib.
5141 }
5142 
5143 - (IBAction)showAlerView:(id)sender {
5144     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"mes" preferredStyle:UIAlertControllerStyleAlert];
5145     UIAlertAction *action = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
5146         UITextField *t = (UITextField *)alertController.textFields[0];
5147         NSLog(@"%@",t.text);
5148     }];
5149     [alertController addAction:action];
5150     [alertController addTextFieldWithConfigurationHandler:nil];
5151     [self presentViewController:alertController animated:YES completion:nil];
5152     
5153 }
5154 ========================================================================================
5155 知识点 
5156 十四、故事板Storyboard
5157 
5158 1.Storyboard  故事板
5159     1.1 为什么需要故事板
5160         因为在使用xib文件制作界面时存在以下两个问题:
5161             a。多个xib之间表达的界面的层级关系,以及界面的行进关系无法在第一时间准确把握
5162             b。代码中会有大量的创建目标控制器,类似于initWithNibName这样的代码,一旦有任何改动,那么所有initWithNibName就都需要修改,行进路线发生变动
5163             基于以上两个问题,希望能够将app看成一个整体,能够清晰的表达界面的行进路线,以及对各个控制器的创建工作能够进行统一管理,于是,从iOS5以后,推出了故事板技术来代替xib
5164 
5165 
5166     1.2 什么是故事板
5167       一个特殊的文件,后缀是storyBoard,本质上还是一个xml文件,用于记录在应用中,各个界面的行进路线,能够以图形化的方式设计界面,并且对于其中使用到的控制器能够自动的创建对象。
5168             故事板是从应用的整体角度来进行设计产品,并且将一部分创建工作实现了编码最大化的自动化。
5169 
5170     1.3 故事板的简单使用
5171             【Demo1_StoryBoard】
5172                 a。程序启动时的流程
5173                         在工程的配置文件中首先要配置Main Interface为故事板,然后在故事板中再设置起始场景,然后启动工程,第一个展示的界面对应的控制器实例就由故事板来自动创建了
5174                 
5175                 b。场景  (Scene)
5176                         指的是故事板中的一个节点。节点中可以包含 控制器、第一响应者、出口以及该场景的过渡(Segue)
5177 
5178                 c。设计界面
5179                     设计方法:与使用xib时没有区别
5180                     连线动作:没有区别
5181                     连线输出口:没有区别
5182 
5183                     重点需要注意的是:一旦需要对场景中的vc进行编码时,一定要单独创建一个没有xib文件的控制器器类,并且将故事板中的场景中的vc与创建的控制器类绑定在一起
5184                     如何绑定:选中 场景,点击第三个检查器,选择class属性,后面的下拉框,找到对应的控制器类即可
5185 
5186     1.4 使用storyBoard配置各种控制器
5187                 a。导航 Editor—>Embed in—>导航/标签
5188                 b。标签
5189                 c。表格
5190                     设置表格的步骤:
5191                 1)选中表视图后,设置表格是static(静态)还是dynamic(动态)的
5192                 2)选中表视图中的 Prototype Cell(原型单元格),在第四个检查其中修改为自定义单元格或者是4种基本样式中的某一个中类型
5193                 3)一定要为Prototype Cell设置identifier,因为这一次的设置相当于原来在viewDidLoad中写的regist。。。注册单元格的代码
5194                 4)为表视图绑定自己创建的继承自UITableViewController的类,回答三问一答
5195                 5)其中,viewDidLoad中已经不需要使用regist方法来注册单元格了,在第三问中,只需要直接从队列中按照在storyBoard中设置的identifier来dequeu单元格即可。
5196                 注意:
5197                         1。identifier区分大小写
5198                         2。deque单元格时,可以使用带有forIndexPath参数的那个方法了
5199                     
5200       1.5 UITableView在故事板中的使用【Demo1】
5201 
5202             a。静态表格的配置
5203                 实现步骤:
5204                     从资源库中添加表格控制器,在第四个检查器中,设置第一个Content为Static Cell
5205                                         修改Prototype 属性的值为0
5206                                         设置section属性 (分区)
5207                                         设置style属性为group
5208                                         逐一选中各个Section设置头和cell的数目
5209                                         逐一选中Cell,设置样式
5210                                         新建一个类,继承自UITableViewController,在故事板中选中控制器,修改第三个检查器中的class属性,将自定义的类与场景关联起来
5211                                 ****删除新建的类中,有关生成表格的三问一答的代码
5212                                 ****如果需要对静态表格中的控件,赋动态值,可以在拆分视图下,将控件连线到类中,然后在viewWillAppear:事件中为控件赋值
5213 5214             b。动态表格的配置
5215                     1)系统原型
5216                             实现步骤:
5217                                         从资源库中添加表格控制器
5218                                         设置表格的Prototype 的个数为1
5219                                         选中Prototype Cell,在检查器中,设置该原型的style及identifier
5220                                         创建一个类,继承自UITableViewController
5221                                         将类与故事板中的控制器绑定
5222                                         在类中回答三问,其中,单元格不需要注册,回答第三问时,直接按照属性栏中设置的identifier去队列中取单元格即可
5223                                         
5224                     2)自定义原型
5225                             实现步骤:
5226                                     从资源库中拖拽一个TableVC
5227                                     添加一个单元格原型view
5228                                     设定该原型的style为Custom
5229                                     设定该原型的identifier
5230                                     设计cell的内容
5231                                     新建一个类,继承自UITableViewCell
5232                                     选中原型cell,第三个检查器,设定class为新建的类
5233                                     拆分视图中,将原型cell中的各个控件连线到类中,作为公开的输出口
5234                                     新建一个类,继承自UITableViewController,并与故事板中的vc绑定
5235                                     回答第三个问题时,从队列中按照原型的identifier取出自定义的cell类的实例,并返回
5236         
5237                     3)混合原型
5238                             实现步骤:
5239                                         在故事板中添加tableVC后,设定Prototype Cell的个数为2
5240                                         为每一个Prototype Cell设定identifier
5241                                         在tableVC绑定的自定义类中,回答三问一答
5242                                         其中,第三问,生成单元格时,可以根据生成的单元格的位置及设定的规律,取队列中,按照不同的identifier来取不同原型的单元格,然后返回
5243 News.h
5244 #import <Foundation/Foundation.h>
5245 
5246 @interface News : NSObject
5247 
5248 @property(nonatomic,strong) NSString *title;
5249 @property(nonatomic,strong) NSString *newsImageName;
5250 @property(nonatomic) NSUInteger commentNumber;
5251 
5252 -(instancetype)initWithTitle:(NSString*)title
5253         andImageName:(NSString*)imageName
5254     andCommentNumber:(NSUInteger)commentNumber;
5255 
5256 +(NSArray *)demoData;
5257 
5258 @end
5259 News.M
5260 #import "News.h"
5261 
5262 @implementation News
5263 
5264 -(instancetype)initWithTitle:(NSString*)title
5265         andImageName:(NSString*)imageName
5266     andCommentNumber:(NSUInteger)commentNumber{
5267     self = [super init];
5268     if (self) {
5269         self.title = title;
5270         self.newsImageName = imageName;
5271         self.commentNumber = commentNumber;
5272     }
5273     return self;
5274     
5275 }
5276 
5277 + (NSArray *)demoData{
5278     News *n1 = [[News alloc]initWithTitle:@"联通被曝高危漏洞 或致用户通话记录等信息泄露" andImageName:@"n1.png" andCommentNumber:186];
5279     News *n2 = [[News alloc]initWithTitle:@"CES2015回顾:民用无人机来袭 中国公司占主导" andImageName:@"n2.png" andCommentNumber:17];
5280     News *n3 = [[News alloc]initWithTitle:@"中企征战CES:难寻颠覆性产品 未打通海外品牌渠道" andImageName:@"n3.png" andCommentNumber:17];
5281     News *n4 = [[News alloc]initWithTitle:@"老话重提:“专车”是否是黑车?被查合不合法" andImageName:@"n4.png" andCommentNumber:178];
5282     News *n5 = [[News alloc]initWithTitle:@"马云告诫员工千万别碰京东:京东将会成悲剧" andImageName:@"n5.png" andCommentNumber:4374];
5283     News *n6 = [[News alloc]initWithTitle:@"三星Q4营业利润47亿美元超预期:内存芯片需求利好" andImageName:@"n6.png" andCommentNumber:6];
5284     News *n7 = [[News alloc]initWithTitle:@"索尼宣布PS4国行版延期上市 或因被举报不锁区" andImageName:@"n7.png" andCommentNumber:0];
5285     News *n8 = [[News alloc]initWithTitle:@"微众银行开业前夕推“信用付”临时核心系统过度" andImageName:@"n8.png" andCommentNumber:4];
5286     
5287     return @[n1,n2,n3,n4,n5,n6,n7,n8];
5288 }
5289 
5290 @end
5291 NewsCell.h
5292 #import <UIKit/UIKit.h>  连线自定义的内容
5293 
5294 @interface NewsCell : UITableViewCell
5295 
5296 @property (weak, nonatomic) IBOutlet UIImageView *newsImageVIew;
5297 @property (weak, nonatomic) IBOutlet UILabel *newsTitleLabel;
5298 @property (weak, nonatomic) IBOutlet UILabel *newsCountLabel;
5299 
5300 @end
5301 NewsCell.M
5302 
5303 MutilProtoViewController.h
5304 MutilProtoViewController.m
5305 #import "MutilProtoViewController.h"
5306 #import "News.h"
5307 #import "NewsCell.h"
5308 
5309 @interface MutilProtoViewController ()
5310 @property(nonatomic,strong)NSArray *allNews;
5311 
5312 @end
5313 
5314 @implementation MutilProtoViewController
5315 
5316 - (NSArray *)allNews{
5317     if (!_allNews) {
5318         _allNews = [News demoData];
5319     }
5320     return _allNews;
5321 }
5322 
5323 
5324 
5325 - (void)viewDidLoad
5326 {
5327     [super viewDidLoad];
5328 
5329 }
5330 
5331 
5332 #pragma mark - Table view data source
5333 
5334 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
5335 {
5336     return 1;
5337 }
5338 
5339 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
5340 {
5341     return self.allNews.count*2;
5342 }
5343 
5344 
5345 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
5346 {
5347     if (indexPath.row % 2) {
5348          NewsCell *newsCell = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
5349         //按照行号找到数据
5350         //得到的标号是: 0 1 2 3 4 5
5351         //实际的行号是: 1 3 5 7 9 11
5352         News *news = self.allNews[indexPath.row/2];
5353         //将新闻实体的各个属性设置到cell中
5354         newsCell.newsImageVIew.image = [UIImage imageNamed:news.newsImageName];
5355         newsCell.newsTitleLabel.text = news.title;
5356         newsCell.newsCountLabel.text = [NSString stringWithFormat:@"%d",news.commentNumber];
5357         return newsCell;
5358     }else{
5359         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1" forIndexPath:indexPath];
5360         cell.textLabel.text = @"订单详情";
5361         return cell;
5362     }
5363 }
5364 
5365 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
5366     if (indexPath.row%2) {
5367         return 100;
5368     }else{
5369         return 44;
5370     }
5371 }
5372 
5373 
5374     1.6跳转 【Demo2_Goto_OtherVC】
5375             
5376             从一个场景跳转到另一个场景(从一个vc跳转到另一个vc)的过程
5377             
5378             实现方式:
5379             方式一:Segue
5380                 不需要编写代码,通过连线的方式来实现
5381                 实现步骤:
5382                     选中原始vc,按住control,按左键,拖拽到目标vc,弹出菜单后,根据跳转的需要选择push或model即可
5383 
5384             方式二:
5385                 从一个vc跳转到故事板中的另一个独立的vc
5386                 实现步骤:
5387                     选中故事板中的vc,第三个检查器中,设定vc的Storyboard ID
5388                     在要执行跳转的事件中编写代码
5389                     获取控制器所在的故事板对象
5390                             self.storyboard
5391                      故事板发送消息instantiateViewControllerWithIdentifier:@"SomeVC"
5392                     其中,参数填写为vc的storyboard ID即可                    然后,推出该控制器实例
5393 - (IBAction)gotoSomeVC:(UIButton *)sender {
5394     //找到故事板创建的vc实例
5395     SomeViewController *someVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SomeVC"];
5396     //推出新vc
5397     [self.navigationController pushViewController:someVC animated:YES];
5398 }
5399 
5400 
5401             方式三:跳转到独立的xib
5402                     实现方式:同以前的跳转方式,使用initWithNibName方法创建vc的实例后,推出即可
5403 - (IBAction)gotoOtherVC:(UIButton *)sender {
5404     //根据xib的名字创建vc
5405     OtherViewController *otherVC = [[OtherViewController alloc]initWithNibName:@"OtherViewController" bundle:nil];
5406     //推出新vc
5407     [self.navigationController pushViewController:otherVC animated:YES];
5408 }
5409 
5410 
5411     1.7 传值
5412            1.7.1   正向传值:从AVC跳转到BVC时,AVC传数据给BVC的过程
5413             实现思路:
5414                         a。BVC要有公开的属性接收数据
5415                         b。AVC在跳转到BVC之前,获取到BVC的引用,就可以为空开的属性赋值,完成正向传值了
5416                                      c.segue.destinationViewController实现跳转
5417 AViewController.h
5418 AViewController.m
5419 #import "AViewController.h"
5420 #import "BViewController.h"
5421 
5422 @interface AViewController ()
5423 @property (weak, nonatomic) IBOutlet UITextField *textFiled; 连线文本框
5424 
5425 @end
5426 
5427 @implementation AViewController
5428 
5429 
5430 - (void)viewDidLoad
5431 {
5432     [super viewDidLoad];
5433 }
5434 
5435 //在跳转到BVC的Segue发生之前
5436 //获取跳转到的目标vc也就是BVC的控制权
5437 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
5438     if ([segue.identifier isEqualToString:@"segueToBVC"]) {  跳转线
5439         //通过segue对象获取到目标vc
5440         BViewController *bvc = segue.destinationViewController;
5441         bvc.codeString = self.textFiled.text;
5442     }
5443 }
5444 BViewController.h
5445 #import <UIKit/UIKit.h>
5446 
5447 @interface BViewController : UIViewController
5448 
5449 @property(nonatomic,strong)NSString *codeString;
5450 
5451 @end
5452 
5453 BViewController.m
5454 #import "BViewController.h"
5455 
5456 @interface BViewController ()
5457 @property (weak, nonatomic) IBOutlet UILabel *codeLabel;
5458 
5459 @end
5460 
5461 @implementation BViewController
5462 
5463 
5464 - (void)viewDidLoad
5465 {
5466     [super viewDidLoad];
5467 
5468 }
5469 
5470 - (void)viewWillAppear:(BOOL)animated
5471 {
5472     self.codeLabel.text = self.codeString;
5473 }
5474 
5475 @end
5476 
5477         1.7.2  反向传值:从AVC跳转到BVC后,BVC返回AVC的过程中给AVC传值的过程
5478             实现思路:
5479                         a。BVC是委托方,做三件事(定义协议,添加delegate属性,合适的时机发消息)
5480                         b。AVC是代理方,做三件事(遵守协议,实现方法,设置BVC的delegate为self)
5481 
5482 **注意:设置BVC的delegate为self时应考虑segue方法,使用如下
5483 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
5484     if ([segue.identifier isEqualToString:@"segueGotoDVC"]) {
5485         DViewController *dvc = segue.destinationViewController;
5486         dvc.delegate = self;
5487     }
5488 }
5489 
5490 ===========================================================
5491 知识点
5492 十五、UICollectionViewController
5493 
5494 1.1 是什么?
5495                 可以控制视图,以多行多列的形式展示数据的一种控制器。是从UITableViewController演变而来,所以在使用上与表视图很像。(从iOS6开始支持)
5496 
5497 1.2 与表视图相比的相同点
5498                 UITableViewController
5499                         .tableView  (UITableView)
5500                             .dataSource (id<UITableViewDataSource>)
5501                             .delegate(id<UITableViewDelegate>)
5502                 每一行的类型是:UITableViewCell
5503                 加载数据:实现三问一答即可(有几个分区,每个分区多少行,每一行什么样)
5504 
5505                 UICollectionViewController
5506                          .collectionView (UICollectionView)
5507                             .dataSource(id<UICollectionViewDataSource>)
5508                             .delegate(id<UICollectionViewDelegate>)
5509                 每一个项的类型:UICollectionViewCell
5510                 加载数据:实现三问一答即可(有几个分区,每个分区多少item,每一个项什么样)
5511 
5512 1.3与tableView相比不同点
5513             a。tableViewCell默认自带了三个显示内容的控件(imageView,textLabel,detailLabel),并且这三个控件的摆放位置也有系统定义好的四种样式,而collectionViewCell没有任何事先由系统定义的用于显示内容的控件,只有能访问的这样几个属性:backgroundColor、backgroundView、contentView
5514         
5515             b。tableViewCell的排布顺序是系统提前定义好的,都是严格的按照从上到下,依次排布,也就是每一个cell的坐标,在指定了行高以后,都有系统计算完成并显示;collectionView没有固定的item的排布规则,需要自定义,这是需要一个特殊的叫做布局类(UICollectionViewLayout)来辅助CollectionView完成每一个item的显示位置的设定,系统只提供了一种布局类,从UICollectionViewLayout派生出来的,叫做流式布局UICollectionFlowLayout,这种系统定义的布局类的布局特点是:一行排满后,自动换行,
5516 
5517 1.4UICollectionViewController的基本使用
5518 
5519     1.1Code 纯代码的方式【Demo1_CollectionView_Code】
5520 
5521         a。Cell的简单样式 + 系统的流式布局
5522                 UICollectionViewCell + UICollectionViewFlowLayout
5523 
5524         b。Cell的简单样式 + 自定义的流式布局
5525                 UICollectionViewCell+MyFlowLayout
5526             
5527         c。自定义Cell + 自定义的流式布局
5528                 MyCell + MyFlowLayout
5529 AppDelegate.h
5530 AppDelegate.m
5531 #import "AppDelegate.h"
5532 #import "MyCollectionViewController.h"
5533 #import "MyFlowLayout.h"
5534 
5535 @implementation AppDelegate
5536 
5537 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
5538 {
5539     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
5540     //创建一个默认的流式布局类的对象
5541     //UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
5542     MyFlowLayout *layout = [[MyFlowLayout alloc]init];
5543     MyCollectionViewController *vc = [[MyCollectionViewController alloc]initWithCollectionViewLayout:layout];
5544     self.window.rootViewController = vc;
5545     [self.window makeKeyAndVisible];
5546     return YES;
5547 }
5548 
5549 MyCell.h
5550 #import <UIKit/UIKit.h>
5551 
5552 @interface MyCell : UICollectionViewCell
5553 @property(nonatomic,strong)UIImageView *bgImageView;
5554 @property(nonatomic,strong)UILabel *label;
5555 
5556 @end
5557 
5558 MyCell.m
5559 #import "MyCell.h"
5560 
5561 @implementation MyCell
5562 
5563 - (id)initWithFrame:(CGRect)frame
5564 {
5565     self = [super initWithFrame:frame];
5566     if (self) {
5567         self.bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
5568         //设置图片视图为cell的背景视图
5569         self.backgroundView = self.bgImageView;
5570         self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
5571         self.label.textAlignment = NSTextAlignmentCenter;
5572         self.label.font = [UIFont boldSystemFontOfSize:30];
5573         //将标签添加到cell的内容视图中
5574         [self.contentView addSubview:self.label];
5575     }
5576     return self;
5577 }
5578 
5579 @end
5580 MyFlowLayout.h
5581 #import <UIKit/UIKit.h>
5582 
5583 @interface MyFlowLayout : UICollectionViewFlowLayout
5584 
5585 @end
5586 MyFlowLayout.m
5587 #import "MyFlowLayout.h"
5588 
5589 @implementation MyFlowLayout
5590 
5591 - (id)init
5592 {
5593     self = [super init];
5594     if (self) {
5595         // 流式布局的自定义设置
5596         self.itemSize = CGSizeMake(80, 80);
5597         self.minimumLineSpacing = 10;
5598         self.minimumInteritemSpacing = 10;
5599         self.sectionInset = UIEdgeInsetsMake(154, 30, 154, 30);
5600         self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
5601     }
5602     
5603     return self;
5604 }
5605 
5606 @end
5607 MyCollectionViewController.h
5608 #import <UIKit/UIKit.h>
5609 
5610 @interface MyCollectionViewController : UICollectionViewController
5611 
5612 @end
5613 MyCollectionViewController.m
5614 #import "MyCollectionViewController.h"
5615 #import "MyCell.h"
5616 
5617 @interface MyCollectionViewController ()
5618 
5619 @end
5620 
5621 @implementation MyCollectionViewController
5622 
5623 static NSString *reuseIdentifier=@"MyCell";
5624 
5625 - (void)viewDidLoad
5626 {
5627     [super viewDidLoad];
5628     //注册CollectionViewCell
5629     [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:reuseIdentifier];
5630 }
5631 // 三问
5632 
5633 // 第一问:多少个分区
5634 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
5635 {
5636     return 3;
5637 }
5638 
5639 // 第二问:每个分区多少个项
5640 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
5641 {
5642     return 9;
5643 }
5644 
5645 // 第三问:每个项什么样
5646 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
5647 {
5648     // 从队列中按标识取cell
5649     MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
5650     // 设置自定义cell的背景图和标签的内容
5651     cell.bgImageView.image = [UIImage imageNamed:@"f.png"];
5652     cell.label.text = [NSString stringWithFormat:@"%d",indexPath.row];
5653     
5654     
5655     
5656     // 设置cell
5657 //    cell.backgroundColor = [UIColor whiteColor];
5658 //    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
5659 //    if (label == nil) {
5660 //        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
5661 //        label.font = [UIFont boldSystemFontOfSize:26];
5662 //        label.textAlignment = NSTextAlignmentCenter;
5663 //        //为label添加tag值
5664 //        label.tag = 1;
5665 //        [cell.contentView addSubview:label];
5666 //    }
5667 //    label.text = [NSString stringWithFormat:@"%d",indexPath.row];
5668 //    
5669     // 返回cell
5670     return  cell;
5671 }
5672 水平滚动显示                                          竖直滚动显示
5673      
5674     1.2 Xib 方式
5675         1).主要就是xib中创建的UICollectionViewController,如果是自动义的布局需在xib的右侧区域与创建的类相连接,并且需注意注册时应该使用nibWithNibName,如:
5676 - (void)viewDidLoad
5677 {
5678     [super viewDidLoad];
5679     //注册cell
5680     //[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
5681     [self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
5682 }
5683 5684        ***上边的图片是在xib中刚开始创建的时候并没有与控制器连接,需通过黄色的方框与控制器连接,并通过第五个检查器连线设置代理
5685 
5686            2).并且注意MyFlowLayout.m中要初始化方法需使用- (id)initWithCoder:(NSCoder *)aDecoder{来设置布局大小,滚动方向等
5687 - (id)initWithCoder:(NSCoder *)aDecoder{
5688     self = [super initWithCoder:aDecoder];
5689     if (self) {
5690         self.itemSize = CGSizeMake(60, 60);
5691         self.minimumInteritemSpacing = 10;
5692         self.minimumLineSpacing = 10;
5693         self.sectionInset  = UIEdgeInsetsMake(100, 25, 100, 25);
5694         self.scrollDirection = UICollectionViewScrollDirectionVertical;
5695     }
5696     return self;
5697 }
5698     3).而MyCell.h类中的单元格内容的设置不需要手写代码了,只需在xib中设置,然后公开属性连线即可(需注意此类创建不自带xib,需自己重新创建,User Interface—>Empty—>next—>起名字,与类名前缀起的一样即可,并添加单元格,在单元格里添加图片和label即可,将class与自己创建的类关联起来)
5699     
5700      1.3 Storyboard方式
5701 
5702          1).故事板中导入UICollectionViewController控制器,全部选中,并将自己创建的类相关联,
5703          2)只选中view,设置自定义和格式,并与创建的类MyFlowLayout相关联,滚动方向及显示大小都通过自己创建的类来设置
5704          3)选中故事板中的cell与自己创建的类相关联class,自己创建的cell类在公开属性中与故事板中创建的模型连接属性即可
5705 5706 2.使用UICollectionViewController实现复杂布局
5707 
5708     2.1 自定义的复杂的流式布局实现水平移动时的缩放
5709 MyFlowLayout.h
5710 MyFlowLayout.m
5711 #import "MyFlowLayout.h"
5712 
5713 @implementation MyFlowLayout
5714 
5715 -(id)init
5716 {
5717     self = [super init];
5718     if (self) {
5719         self.itemSize = CGSizeMake(200, 200);
5720         self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
5721         self.sectionInset = UIEdgeInsetsMake(60, 0, 60, 0);
5722         self.minimumLineSpacing = 100;
5723         
5724     }
5725     return self;
5726 }
5727 
5728 
5729 -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
5730     return YES;
5731 }
5732 
5733 必须使用这个方法进行缩放
5734 -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
5735     NSArray *array = [super layoutAttributesForElementsInRect:rect];
5736     //创建可视化的矩形区域
5737     CGRect visiabelRect = CGRectZero;
5738     visiabelRect.origin = self.collectionView.contentOffset;
5739     visiabelRect.size = self.collectionView.bounds.size;
5740     //可视化矩形区域的中心点x
5741     CGFloat visiableRectCenterX = CGRectGetMidX(visiabelRect);
5742     //依次获取系统指定的矩形区域中的每一个item
5743     //的中心点坐标
5744     //将可视化区域的中心点与item的中心点进行
5745     //比对,根据两个中心点的距离产生一个变化的
5746     //比率,并将该比率作为item的缩放比率即可
5747     for (UICollectionViewLayoutAttributes *attributes in array) {
5748         //获取每一个item的中心点
5749         CGFloat itemCenterX = attributes.center.x;
5750         //计算两个中心点的距离
5751         CGFloat distance = visiableRectCenterX - itemCenterX;
5752         //设定两个中心点的距离在200以内时
5753         //才针对item做放大缩小的操作
5754         if (ABS(distance) < 200) {
5755             //根据distance的大小产生一个变化的zoomFator缩放因子
5756             CGFloat zoomFactor = 1 + 0.5*(1-ABS(distance/200.0));
5757             attributes.transform3D = CATransform3DMakeScale(zoomFactor, zoomFactor, 1);
5758         }
5759     }
5760     return array;
5761 }
5762 
5763 @end
5764 
5765     2.2 自定义的不规则布局
5766 
5767 
5768 补充:
5769 
5770 1.frame属性描述的是,视图在父视图中的位置及所占的区域大小
5771 
5772 2.bounds属性描述的是,视图本身的大小
5773 
5774 3.屏幕:3.5320 高480
5775     屏幕:4320 高568
5776 
5777 练习: 水平滚动的  三页九宫格
5778                 垂直滚动的    三页九宫格
5779 
5780                 九宫格间距较紧密,居中显示
5781 
5782 水平滚动:(4寸)
5783 itemSize:80 X 80
5784 itemSpacing:10
5785 lineSpacing:10
5786 sectionInset:top:[568 - (80*3+10*2)]/2=154
5787                                   left:[320 - (80*3+10*2)]/2= 30
5788 总结:
5789 
5790  如果要利用UICollectionViewController来设计界面,需要创建3个类来实现此功能
5791 MyCollectionViewController.h
5792 MyCollectionViewController.m
5793         此类中主要用于实现三问一答,继承自UICollectionViewController用来创建表格的基本模式,几个分区,几列以及表格的显示内容(需注意:表格的内容需在viewDidLoad中注册才能创建),实现表格的重用
5794 MyFlowLayout.h
5795 MyFlowLayout.m
5796     此类中主要是为了布局,继承自UICollectionViewFlowLayout重写初始化方法,设置显示的大小,项目间的距离、行高、以及水平滚动还是竖直滚动,以及与屏幕的上下左右的间距。
5797         self.itemSize  设置每个单元格的大小       
5798         self.scrollDirection  设置水平滚动还是竖直滚动
5799         self.sectionInset 设置距离屏幕上下左右的距离               
5800         self.minimumLineSpacing 设置行间距
5801         self.minimumInteritemSpacing 设置项目之间的距离
5802 MyCell.h
5803 MyCell.m
5804     此类中主要用来设置单元格的内容,继承自UICollectionViewCell在.h文件中设置一个公开的属性,与MyCollectionViewController联系,一般情况下设置图片背景和标签,在initWithFrame方法中设置。
5805 如:
5806 - (id)initWithFrame:(CGRect)frame
5807 {
5808     self = [super initWithFrame:frame];
5809     if (self) {
5810         self.bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
5811         self.backgroundView = self.bgImageView;
5812         //边框线条宽度
5813         self.contentView.layer.borderWidth = 1.0f;
5814         //边框的颜色
5815         self.contentView.layer.borderColor = [UIColor whiteColor].CGColor;
5816     }
5817     return self;
5818 }
5819 =========================================================================================================
5820     



posted on 2015-12-21 13:32  A蜗牛为梦想而生A  阅读(2736)  评论(1编辑  收藏  举报