UI基础-图片浏览器-改进2

  继续改进上面文章中的图片浏览器小案例,将数据放到字典中.

  首先了解一下,viewDidLoad方法的作用:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
@end

    在视图加载后被调用:
  如果是在代码中创建的视图加载器,他将会在loadView方法后被调用; 如果是从nib视图页面输出,他将会在视图设置好后后被调用

  视图加载完毕后调用,一般用来初始化控件,系统主动调用

  下面附上此次改进的代码:

  ViewController.m

  

  1 //
  2 //  ViewController.m
  3 //  01-图片浏览器1
  4 //
  5 //  Created by hukezhu on 15/5/12.
  6 //
  7 //
  8 
  9 #import "ViewController.h"
 10 #define icon @"icon"
 11 #define desc @"desc"
 12 
 13 @interface ViewController ()
 14 /**  界面上最上面显示索引的label*/
 15 @property (weak, nonatomic) IBOutlet UILabel *noLabel;
 16 /**  显示图片的控件*/
 17 @property (weak, nonatomic) IBOutlet UIImageView *iconView;
 18 /**  描述图片内容的label*/
 19 @property (weak, nonatomic) IBOutlet UILabel *descLabel;
 20 /**
 21  *  左边按钮的属性,用来控制按钮的不可用状态
 22  */
 23 @property (weak, nonatomic) IBOutlet UIButton *left;
 24 /**
 25  *  右边按钮的属性,用来控制按钮的不可用状态
 26  */
 27 @property (weak, nonatomic) IBOutlet UIButton *right;
 28 /**
 29  *  索引,记录位置
 30  */
 31 @property (nonatomic ,assign)int index;
 32 /**
 33  *  存储数据的数组
 34  */
 35 @property (nonatomic,strong)NSArray *images;
 36 /**
 37  *  上一张的点击响应方法
 38  */
 39 - (IBAction)previous;
 40 /**
 41  *  下一张的点击响应方法
 42  */
 43 - (IBAction)next;
 44 @end
 45 
 46 @implementation ViewController
 47 
 48 - (void)viewDidLoad {
 49     [super viewDidLoad];
 50     //创建字典,将数据存到字典中
 51     NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
 52     dict1[icon]=  @"biaoqingdi" ;
 53     dict1[desc] = @"在他面前,其他神马表情都弱爆了!";
 54     NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
 55     dict2[icon]=  @"wangba" ;
 56     dict2[desc] = @"哥们为什么选八号呢";
 57     NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
 58     dict3[icon]=  @"bingli" ;
 59     dict3[desc] = @"这也忒狠了!";
 60     NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];
 61     dict4[icon]=  @"chiniupa" ;
 62     dict4[desc] = @"这小姑娘吃个牛排比杀牛还费劲啊";
 63     NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];
 64     dict5[icon]=  @"danteng" ;
 65     dict5[desc] = @"亲,你能改下你的网名么?哈哈";
 66     _images = @[dict1,dict2,dict3,dict4,dict5];
 67     
 68     //首先将当前计数器设置为-1,然后再调用next方法,即可显示首页面.
 69     //self.index = -1;
 70     //[self next];
 71     [self move];
 72 }
 73 //发现代码中previous和next方法中的代码基本上一样,属于重复性的,可以将其封装成一个方法
 74 
 75 - (void)move{
 76     
 77     //创建一个字典,接收当前索引下的数据
 78     NSDictionary *dict = self.images[self.index];
 79     
 80     //设置noLabel的内容
 81     self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1,self.images.count];
 82     //设置图片
 83     self.iconView.image = [UIImage imageNamed:dict[icon]];
 84     //设置图片描述
 85     self.descLabel.text = dict[desc];
 86     
 87     
 88     
 89     //设置按钮的不可用状态,当计数器为0时,左边的按钮为不可用状态
 90     self.left.enabled = self.index != 0;
 91     //当计数器为4时,右边的按钮为不可用状态
 92     self.right.enabled = self.index != (self.images.count - 1);
 93 }
 94 - (IBAction)previous{
 95     
 96     //计数器减一
 97     self.index--;
 98     [self move];
 99 }
100 - (IBAction)next{
101     self.index++;
102     [self move];
103 }
104 @end

  分析上面代码,发现代码还是有些问题,将数据放到viewDidLoad中,程序一加载就去加载数据,会影响性能,所以,我们当使用到数据的时候,再去加载数据,可以提高性能.由此可以提出"懒加载"的概念.重写数组的get方法.当数据用到时再进行加载,好处是提高性能.

  下篇文章,我会再次改进,使用懒加载的方式加载数据.

posted @ 2015-05-13 16:13  吃唐僧肉的小悟空  阅读(185)  评论(0编辑  收藏  举报