ios开发之个人笔记(通过plist文件展示单组数据LOL)

最终效果:
这里写图片描述

个人步骤:
1.导入素材
2.根据plist创建模型
在模型中添加初始化方法
懒加载获取数据
3.设置UITableView的数据源对象
遵守数据源协议,实现数据源方法

模型类

#import <UIKit/UIKit.h>

@interface YZHeros : UIView

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *intro;

@property (nonatomic,copy) NSString *icon;

- (instancetype) initWithDict:(NSDictionary *)dict;
+ (instancetype) heroWithDict:(NSDictionary *)dict;
@end
#import "YZHeros.h"

@implementation YZHeros

- (instancetype) initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+ (instancetype) heroWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}

@end

ViewController类

#import "ViewController.h"
#import "YZHeros.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSArray *heros;

@end

@implementation ViewController

#pragma mark 懒加载
-(NSArray *)heros{
    if (_heros == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *arrModel = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in arrDict) {
            YZHeros *model = [YZHeros heroWithDict:dict];
            [arrModel addObject:model];
        }
        _heros = arrModel;
    }
    return _heros;
}
#pragma mark 实现每组几行方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.heros.count;
}
#pragma mark 设置内容方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.获取数据
    YZHeros *model = self.heros[indexPath.row];
    //2.添加UITableViewCell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    //3.填充数据
    cell.imageView.image = [UIImage imageNamed:model.icon];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.intro;
    //单元格右边显示小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //4.返回UITableViewCell
    return cell;
}

#pragma mark 隐藏、显示状态栏方法
-(BOOL)prefersStatusBarHidden{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置单元格行高
    //这是设置设置行高一样的方法
    //self.tableView.rowHeight = 80;
    //对于不同行高,通过代理实现
    self.tableView.delegate = self;

}

#pragma mark 实现代理方法设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row % 2 == 0) {
        return 50;
    }else{
        return 80;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

单组数据显示LOL素材

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值