1、iOS开发UI篇UITableview控件基本使用iOS开发UI篇UITableview控件基本使用iOS开发UI篇UITableview控件基本使用 一、一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 复制代码 1 #import Foundation/Foundation.h 2 3 interface NJHero : NSObject 4 /* 5 * 头像 6 */ 7 property (nonatomic, copy) NSString *icon; 8 /*iOS开发UI篇UITableview控件基本使用一、一个简单的英雄展示程序NJHero.h文件代码(字典
2、转模型)复制代码1 #import 23 interface NJHero : NSObject4 /*5 * 头像6 */7 property (nonatomic, copy) NSString *icon;8 /*9 * 名称10 */11 property (nonatomic, copy) NSString *name;12 /*13 * 描述14 */15 property (nonatomic, copy) NSString *intro;1617 - (instancetype)initWithDict:(NSDictionary *)dict;18 + (instancety
3、pe)heroWithDict:(NSDictionary *)dict;19 end复制代码NJViewController.m文件代码复制代码1 #import NJViewController.h2 #import NJHero.h34 interface NJViewController ()5 /*6 * 保存所有的英雄数据7 */8 property (nonatomic, strong) NSArray *heros;9 property (weak, nonatomic) IBOutlet UITableView *tableView;1011 end1213 implemen
4、tation NJViewController1415 #pragma mark - 懒加载16 - (NSArray *)heros17 18 if (_heros = nil) 19 / 1.获得全路径20 NSString *fullPath = NSBundle mainBundle pathForResource:heros ofType:plist;21 / 2.更具全路径加载数据22 NSArray *dictArray = NSArray arrayWithContentsOfFile:fullPath;23 / 3.字典转模型24 NSMutableArray *models
5、 = NSMutableArray arrayWithCapacity:dictArray.count;25 for (NSDictionary *dict in dictArray) 26 NJHero *hero = NJHero heroWithDict:dict;27 models addObject:hero;28 29 / 4.赋值数据30 _heros = models copy;31 32 / 4.返回数据33 return _heros;34 3536 - (void)viewDidLoad37 38 super viewDidLoad;39 / 设置Cell的高度40 /
6、当每一行的cell高度一致的时候使用属性设置cell的高度41 self.tableView.rowHeight = 60;42 self.tableView.delegate = self;43 4445 #pragma mark - UITableViewDataSource46 / 返回多少组47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView48 49 return 1;50 51 / 返回每一组有多少行52 - (NSInteger) tableView:(UITableView *)tableVie
7、w numberOfRowsInSection:(NSInteger)section53 54 return self.heros.count;55 56 / 返回哪一组的哪一行显示什么内容57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath58 59 / 1.创建CELL60 UITableViewCell *cell = UITableViewCell alloc initWithStyle:UITableViewCellStyleS
8、ubtitle reuseIdentifier:nil;61 / 2.设置数据62 / 2.1取出对应行的模型63 NJHero *hero = self.herosindexPath.row;64 / 2.2赋值对应的数据65 cell.textLabel.text = hero.name;66 cell.detailTextLabel.text = hero.intro;67 cell.imageView.image = UIImage imageNamed:hero.icon;68 / 3.返回cell69 return cell;70 71 #pragma mark - UITable
9、ViewDelegate72 /*73 / 当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度74 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath75 76 if (1 = indexPath.row) 77 return 180;78 79 return 44;80 81 */8283 #pragma mark - 控制状态栏是否显示84 /*85 * 返回YES代表隐藏状态栏, NO相反86 */87 - (BOOL)prefers
10、StatusBarHidden88 89 return YES;90 91 end复制代码实现效果:代码注意点:(1)在字典转模型的代码处用下面的代码,为可变数组分配dictArray.count个存储空间,可以提高程序的性能NSMutableArray *models = NSMutableArrayarrayWithCapacity:dictArray.count;(2)设置cell的高度有三种办法可以设置cell的高度1) 可以在初始加载方法中设置,self.tableView.rowHeight = 60;这适用于当每一行的cell高度一致的时候,使用属性设置cell的高度。2)在st
11、oryboard中设置,适用于高度一致3)当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath if (1 = indexPath.row) return 180; return 44;二、cell的一些属性代码示例:复制代码 1 #import NJViewController.h 2 #import NJHero.h 3 4 interface NJViewController () 5
12、/* 6 * 保存所有的英雄数据 7 */ 8 property (nonatomic, strong) NSArray *heros; 9 property (weak, nonatomic) IBOutlet UITableView *tableView;1011 end1213 implementation NJViewController1415 #pragma mark - 懒加载16 - (NSArray *)heros17 18 if (_heros = nil) 19 / 1.获得全路径20 NSString *fullPath = NSBundle mainBundle pa
13、thForResource:heros ofType:plist;21 / 2.更具全路径加载数据22 NSArray *dictArray = NSArray arrayWithContentsOfFile:fullPath;23 / 3.字典转模型24 NSMutableArray *models = NSMutableArray arrayWithCapacity:dictArray.count;25 for (NSDictionary *dict in dictArray) 26 NJHero *hero = NJHero heroWithDict:dict;27 models add
14、Object:hero;28 29 / 4.赋值数据30 _heros = models copy;31 32 / 4.返回数据33 return _heros;34 3536 - (void)viewDidLoad37 38 super viewDidLoad;39 / 设置Cell的高度40 / 当每一行的cell高度一致的时候使用属性设置cell的高度41 self.tableView.rowHeight = 60;42 self.tableView.delegate = self;4344 4546 #pragma mark - UITableViewDataSource47 / 返回
15、多少组48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView49 50 return 1;51 52 / 返回每一组有多少行53 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section54 55 return self.heros.count;56 57 / 返回哪一组的哪一行显示什么内容58 - (UITableViewCell *)tableView:(UITableView *)tab
16、leView cellForRowAtIndexPath:(NSIndexPath *)indexPath59 60 / 1.创建CELL61 UITableViewCell *cell = UITableViewCell alloc initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil;62 / 2.设置数据63 / 2.1取出对应行的模型64 NJHero *hero = self.herosindexPath.row;65 / 2.2赋值对应的数据66 cell.textLabel.text = hero.name;
17、67 cell.detailTextLabel.text = hero.intro;68 cell.imageView.image = UIImage imageNamed:hero.icon;69 70 / 2.3设置cell的辅助视图71 / cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;72 if (0 = indexPath.row) 73 cell.accessoryView = UIButton buttonWithType:UIButtonTypeContactAdd;74 else75 76 c
18、ell.accessoryView = UISwitch alloc init;77 78 / UIButton *btn = UIButton alloc init;79 / btn.backgroundColor = UIColor redColor;80 / cell.accessoryView = btn;81 82 83 / 2.4设置cell的背景颜色84 cell.backgroundColor = UIColor blueColor;85 86 / 设置默认状态的背景87 / UIView *view = UIView alloc init;88 / view.backgrou
19、ndColor = UIColor blueColor;89 / cell.backgroundView = view;90 91 UIImageView *iv = UIImageView alloc initWithImage:UIImage imageNamed:buttondelete;92 cell.backgroundView = iv;93 94 / 设置选中状态的背景95 UIView *view2 = UIView alloc init;96 view2.backgroundColor = UIColor purpleColor;97 cell.selectedBackgroundView = view2;98 / 3.返回cell99 return cell;100 101102103 #pragma mark - 控制状态栏是否显示104 /*105 * 返回YES代表隐藏状态栏, NO相反106 */107 - (BOOL)prefersStatusBarHidden108 109 return YES;110 111 end
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1