UITableView-03LOL英雄

来源:互联网 发布:动作电影推荐 知乎 编辑:程序博客网 时间:2024/04/29 17:46

实现如下界面

这里写图片描述

  • 注意点:

    • 在StroryBoard中拖线,设置tableView的数据源
    • 不要把UITableViewDataSource写出UITableViewDelegate
    • 添加图片资源和plist到Assets.xcassets
  • 资源地址:度云 链接: http://pan.baidu.com/s/1pLAaRlL 密码: hqau

  • ViewController.m 源文件

////  ViewController.m//  LOL英雄-单组数据////  Created by Kavee DJ on 16/5/13.//  Copyright © 2016年 Kavee DJ. All rights reserved.//#import "ViewController.h"#import "KDJHero.h"@interface ViewController () <UITableViewDataSource>@property (nonatomic, strong) NSArray *heroes;@end@implementation ViewController// 懒加载- (NSArray *)heroes{    if (_heroes == nil) {        // 加载plist中的字典数组        NSString *path = [[NSBundle mainBundle] pathForResource:@"heroes.plist" ofType:nil];        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];        // 字典数组 -> 模型数组        NSMutableArray *heroArray = [NSMutableArray array];        for (NSDictionary *dict in dictArray) {            KDJHero *hero = [KDJHero heroWithDict:dict];            [heroArray addObject:hero];        }        _heroes = heroArray;    }    return _heroes;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - <UITableViewDataSource>// 默认就是1组//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//{//    return 1;//}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.heroes.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];    // 取出每一行对应的数据模型    KDJHero *hero = self.heroes[indexPath.row];    cell.textLabel.text = hero.name;    cell.imageView.image = [UIImage imageNamed:hero.icon];    cell.detailTextLabel.text = hero.intro;    return cell;}@end
0 0
原创粉丝点击