国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[iOS基礎控件-4.3]APP列表xib的使用

2019-11-14 19:50:03
字體:
來源:轉載
供稿:網友
A.storyboard和xib
1.storyboard: 相對xib較重量級,控制整個應用的所有界面
2.xib: 輕量級,一般用來描述局部界面
 
B.使用
1.新建xib文件
New File ==> User Interface ==> Empty
Image
 
2.打開新建的xib文件,出現可視化窗口
(1)拖入一個UIView (不是UIViewController)
(2)設置大小:開啟可自定義尺寸 ==> 定義尺寸
Image
(3)拖入圖標圖片、名字、下載按鈕,調整設置
Image
 
3.在代碼中獲取xib中的view,并設置數據
(1)從xib獲取view
a.方法1:
1         // 1.獲取xib中的view, xib中可以同時定義多個view,注意名字不帶擴展名2         NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed:@"app" owner:nil options:nil];3         UIView *appView = [viewArray lastObject];
 
b.方法2:
1         UINib *nib = [UINib nibWithNibName:@"app" bundle:[NSBundle mainBundle]];2         NSArray *viewArray = [nib instantiateWithOwner:nil options:nil];3         UIView *appView = [viewArray lastObject];
 
(2)取出View中的元素,設置圖片
a.方法1,使用SubView數組:
1         // 3.設置圖片2         UIImageView *iconView = appView.subviews[2];3         iconView.image = [UIImage imageNamed:appData.icon];
 
注意:按照教程是按照下圖的順序排列數組元素(imageView應該是subviews[0],但是實際編程發現卻不是,所以此方法并不穩定)
Image
 
b.方法2,使用tag:
1         // 3.設置圖片2         UIImageView *iconView = [appView viewWithTag:1];3         iconView.image = [UIImage imageNamed:appData.icon];
 
(3)設置名字
1         // 4.設置名字2         UILabel *nameLabel = [appView viewWithTag:2];3         nameLabel.text = appData.name;
 
(4)下載按鈕已經在xib中定義好,不必使用代碼
 
 
C.實現代碼
  1 #import "ViewController.h"  2 #import "App.h"  3   4 #define ICON_KEY @"icon"  5 #define NAME_KEY @"name"  6 #define APP_WIDTH 85  7 #define APP_HEIGHT 90  8 #define MARGIN_HEAD 20  9 #define ICON_WIDTH 50 10 #define ICON_HEIGHT 50 11 #define NAME_WIDTH APP_WIDTH 12 #define NAME_HEIGHT 20 13 #define DOWNLOAD_WIDTH (APP_WIDTH - 20) 14 #define DOWNLOAD_HEIGHT 20 15  16 @interface ViewController () 17  18 /** 存放應用信息 */ 19 @PRoperty(nonatomic, strong) NSArray *apps; // 應用列表 20  21 @end 22  23 @implementation ViewController 24  25 - (void)viewDidLoad { 26     [super viewDidLoad]; 27     // Do any additional setup after loading the view, typically from a nib. 28      29     [self loadApps]; 30 } 31  32 - (void)didReceiveMemoryWarning { 33     [super didReceiveMemoryWarning]; 34     // Dispose of any resources that can be recreated. 35 } 36  37 #pragma mark 取得應用列表 38 - (NSArray *) apps { 39     if (nil == _apps) { 40         // 1.獲得plist的全路徑 41         NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; 42          43         // 2.加載數據 44         NSArray *dictArray  = [NSArray arrayWithContentsOfFile:path]; 45          46         // 3.將dictArray里面的所有字典轉成模型,放到新數組中 47         NSMutableArray *appArray = [NSMutableArray array]; 48         for (NSDictionary *dict in dictArray) { 49             // 3.1創建模型對象 50             App *app = [App appWithDictionary:dict]; 51              52             // 3.2 添加到app數組中 53             [appArray addObject:app]; 54         } 55          56         _apps = appArray; 57     } 58  59     return _apps; 60 } 61  62 #pragma mark 加載全部應用列表 63 - (void) loadApps { 64     int appColumnCount = [self appColumnCount]; 65     int appRowCount = [self appRowCount]; 66      67     CGFloat marginX = (self.view.frame.size.width - APP_WIDTH * appColumnCount) / (appColumnCount + 1); 68     CGFloat marginY = (self.view.frame.size.height - APP_HEIGHT * appRowCount) / (appRowCount + 1) + MARGIN_HEAD; 69      70     int column = 0; 71     int row = 0; 72     for (int index=0; index<self.apps.count; index++) { 73         App *appData = self.apps[index]; 74  75         // 1.獲取xib中的view, xib中可以同時定義多個view,注意名字不帶擴展名 76 //        NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed:@"app" owner:nil options:nil]; 77 //        UIView *appView = [viewArray lastObject]; 78          79         UINib *nib = [UINib nibWithNibName:@"app" bundle:[NSBundle mainBundle]]; 80         NSArray *viewArray = [nib instantiateWithOwner:nil options:nil]; 81         UIView *appView = [viewArray lastObject]; 82          83         // 2.定義每個app的位置、尺寸 84         CGFloat appX = marginX + column * (marginX + APP_WIDTH); 85         CGFloat appY = marginY + row * (marginY + APP_HEIGHT); 86         appView.frame = CGRectMake(appX, appY, APP_WIDTH, APP_HEIGHT); 87          88         // 3.設置圖片 89         UIImageView *iconView = [appView viewWithTag:1]; 90         iconView.image = [UIImage imageNamed:appData.icon]; 91          92         // 4.設置名字 93         UILabel *nameLabel = [appView viewWithTag:2]; 94         nameLabel.text = appData.name; 95          96         // 5.加入此app信息到總view 97         [self.view addSubview:appView]; 98          99         column++;100         if (column == appColumnCount) {101             column = 0;102             row++;103         }104     }105 }106 107 108 #pragma mark 計算列數109 - (int) appColumnCount {110     int count = 0;111     count = self.view.frame.size.width / APP_WIDTH;112     113     if ((int)self.view.frame.size.width % (int)APP_WIDTH == 0) {114         count--;115     }116     117     return count;118 }119 120 #pragma mark 計算行數121 - (int) appRowCount {122     int count = 0;123     count = (self.view.frame.size.height - MARGIN_HEAD) / APP_HEIGHT;124     125     if ((int)(self.view.frame.size.height - MARGIN_HEAD) % (int)APP_HEIGHT == 0) {126         count--;127     }128     129     return count;130 }131 132 @end

 

 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九台市| 新竹县| 民乐县| 东丽区| 台前县| 民和| 长泰县| 桂林市| 思茅市| 康马县| 江门市| 古丈县| 乳源| 新兴县| 措美县| 克东县| 登封市| 黑水县| 通化市| 齐河县| 开平市| 樟树市| 武平县| 宁城县| 古丈县| 嘉荫县| 高陵县| 松江区| 开封市| 博乐市| 象山县| 伽师县| 独山县| 建瓯市| 宣恩县| 喀喇沁旗| 铁岭县| 忻城县| 文成县| 托里县| 余干县|