猫猫学IOS(十二)UI之UITableView学习(上)LOL英雄联盟练习

猫猫分享,必须精品

素材代码地址: 原文地址:?viewmode=contents

先看效果图

源代码NYViewController的代码//ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。 原文:http:#import “NYViewController.h”#NYViewController () <UITableViewDataSource,UITableViewDelegate>@property (strong,nonatomic) UITableView *tableView;@property (strong,nonatomic) NSArray *heros;@end@implementation NYViewController-(NSArray *)heros{if (_heros == nil)_heros = [NYHero heros];return _heros;}/**懒加载tableView */-(UITableView *)tableView{if (_tableView == nil) {_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//添加数据源->加入协议_tableView.dataSource = self;/*设置行高方法有两种,代理方法的优先级比setRowHeight的优先级高。应用场景,很多应用程序,每一行高度是不一样的,例如:新浪微博*///_tableView.rowHeight = 80;//第一种_tableView.delegate = self;//第二种,要设置代理,-》协议 -》实现代理方法[self.view addSubview:_tableView];}return _tableView;}- (void)viewDidLoad{[super viewDidLoad];[self tableView];}#pragma mark – 数据源方法/**每个分组中的数据总数*/-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return self.heros.count;}/**告诉表格,每个单元格的明细*/-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{/*UITableViewCellStyleDefault默认类型 标题+可选图像UITableViewCellStyleValue1标题+明细+图像UITableViewCellStyleValue2不显示图像,标题+明细UITableViewCellStyleSubtitle标题+明细+图像*/NSLog(@”表格行明细 %d”,indexPath.row);NSString *ID = @”cell”;//1,去缓存池查找可重复用得单元格UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];//2,如果没找到if (cell == nil) {//NSLog(@”实例化单元格”);cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;}//设置单元格内容// 取出英雄对象NYHero *hero = self.heros[indexPath.row];//设置标题 :英雄名字cell.textLabel.text = hero.name;//设置详细内容: 英雄描述cell.detailTextLabel.text = hero.intro;//设置英雄图标cell.imageView.image = [UIImage imageNamed:hero.icon];/**通常accessoryType提供的类型不能满足时,才会使用自定义控件但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中!!!自定义控件的事件触发,同样不会影响表格行的选中!*/cell;}-(void)switchChanged:(UISwitch *) sender{NSLog(@”%s %@”, __func__, sender);}#pragma mark – 实现代理方法 (行高设置)/**设置行高,比setRowHeight优先级高*/-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 70;}- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@”%s %@”, __func__, indexPath);}/**选中了某一行*/-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@”%s %@”,__func__, indexPath);}/** accessoryType为按钮时,UITableViewCellAccessoryDetailButton点击右侧按钮的监听方法此方法不会触发行选中,跟行选中各自独立只是为accessoryType服务,对自定义控件不响应 */- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{NSLog(@”%s %@”, __func__, indexPath);}@end模型的代码的代码(nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *icon;@property (nonatomic, copy) NSString *intro;-(instancetype) initWithDict:(NSDictionary *)dict;+(instancetype) heroWithDict:(NSDictionary *)dict;+(NSArray *) heros;@end

m实现文件

@implementation NYHero-(instancetype)initWithDict:(NSDictionary *)dict{self = [super init];if (self) {[self setValuesForKeysWithDictionary:dict];}return self;}+(instancetype)heroWithDict:(NSDictionary *)dict{return [[self alloc] initWithDict:dict];}+(NSArray *)heros{NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@”heros.plist” ofType:nil]];NSMutableArray *arrayM = [NSMutableArray array];for (NSDictionary *dict in array) {//这才是应该写的[arrayM addObject:[self heroWithDict:dict]];}return arrayM;}@end猫猫犯二了——关于字典模型的类方法用敬业的精神去面对每一份挑战,

猫猫学IOS(十二)UI之UITableView学习(上)LOL英雄联盟练习

相关文章:

你感兴趣的文章:

标签云: