标签:style blog io ar color os 使用 sp for
1 // 1.获取xib中的view, xib中可以同时定义多个view,注意名字不带扩展名 2 NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed:@"app" owner:nil options:nil]; 3 UIView *appView = [viewArray lastObject];
1 UINib *nib = [UINib nibWithNibName:@"app" bundle:[NSBundle mainBundle]]; 2 NSArray *viewArray = [nib instantiateWithOwner:nil options:nil]; 3 UIView *appView = [viewArray lastObject];
1 // 3.设置图片 2 UIImageView *iconView = appView.subviews[2]; 3 iconView.image = [UIImage imageNamed:appData.icon];
1 // 3.设置图片 2 UIImageView *iconView = [appView viewWithTag:1]; 3 iconView.image = [UIImage imageNamed:appData.icon];
1 // 4.设置名字 2 UILabel *nameLabel = [appView viewWithTag:2]; 3 nameLabel.text = appData.name;
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
标签:style blog io ar color os 使用 sp for
原文地址:http://www.cnblogs.com/hellovoidworld/p/4120782.html