zoukankan      html  css  js  c++  java
  • UI基础-图片浏览器-改进3

      本篇文章再次改进图片浏览器小实例.

      本篇引入"懒加载"的概念.懒加载(延迟加载),也就是在需要的时候才加载(效率高,占用内存小)。所谓懒加载,也就是重写其get方法.

      直接附上源码:

      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 //引入懒加载的概念,重写数组的get方法.当数据用到时再进行加载,好处是提高性能
     74 -(NSArray *)images{
     75     
     76     //首先判断images是否存在数据,不存在则创建
     77     //注意此处不能使用self.images,因为同是get方法,会死循环
     78     if(_images == nil){
     79         
     80         NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
     81         dict1[icon]=  @"biaoqingdi" ;
     82         dict1[desc] = @"在他面前,其他神马表情都弱爆了!";
     83         NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
     84         dict2[icon]=  @"wangba" ;
     85         dict2[desc] = @"哥们为什么选八号呢";
     86         NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
     87         dict3[icon]=  @"bingli" ;
     88         dict3[desc] = @"这也忒狠了!";
     89         NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];
     90         dict4[icon]=  @"chiniupa" ;
     91         dict4[desc] = @"这小姑娘吃个牛排比杀牛还费劲啊";
     92         NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];
     93         dict5[icon]=  @"danteng" ;
     94         dict5[desc] = @"亲,你能改下你的网名么?哈哈";
     95         _images = @[dict1,dict2,dict3,dict4,dict5];
     96         
     97     }
     98     return _images;
     99 }
    100 
    101 
    102 //发现代码中previous和next方法中的代码基本上一样,属于重复性的,可以将其封装成一个方法
    103 - (void)move{
    104     
    105     //创建一个字典,接收当前索引下的数据
    106     NSDictionary *dict = self.images[self.index];
    107     
    108     //设置noLabel的内容
    109     self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1,self.images.count];
    110     //设置图片
    111     self.iconView.image = [UIImage imageNamed:dict[icon]];
    112     //设置图片描述
    113     self.descLabel.text = dict[desc];
    114     
    115     
    116     
    117     //设置按钮的不可用状态,当计数器为0时,左边的按钮为不可用状态
    118     self.left.enabled = self.index != 0;
    119     //当计数器为4时,右边的按钮为不可用状态
    120     self.right.enabled = self.index != (self.images.count - 1);
    121 }
    122 - (IBAction)previous{
    123     
    124     //计数器减一
    125     self.index--;
    126     [self move];
    127 }
    128 - (IBAction)next{
    129     self.index++;
    130     [self move];
    131 }
    132 @end

      分析代码,我们直接将数据写在了代码中,假设以后修改的时候还需要来修改源代码,这样扩展性不好,我们可以将数据存储在plist文件中,我们通过plist文件加载数据.

  • 相关阅读:
    Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’
    An example of polybase for Oracle
    use azure data studio to create external table for oracle
    Missing MSI and MSP files
    You may fail to backup log or restore log after TDE certification/key rotation.
    Password is required when adding a database to AG group if the database has a master key
    Use KTPASS instead of adden to configure mssql.keytab
    ardunio+舵机
    android webview 全屏100%显示图片
    glide 长方形图片显示圆角问题
  • 原文地址:https://www.cnblogs.com/hukezhu/p/4500842.html
Copyright © 2011-2022 走看看