UI之 06 TableView 02 单组数据展示(lol英雄展示)

2.展示单组数据

展示lol英雄

预先效果:

Snip20160314_5.png

同样的, 我们需要先设置,三个方法.

注意:由于我们的是设置单组数据, 所以, 我们实际上是两种方法, 第一个方法是: 设置一共有多少组, 现在你由于我们确定了只有一组.

注意:不要忘记了,遵守协议: <UITableViewDataSource, UITableViewDelegate>

1. 材料准备

  • 我们需要一个为英雄简述plist文档
  • 我们需要一些英雄的图片

2. storyboard设置

storyboard中, 我们要拖入一TableView,然后将其设置为我们控制器的一个方法

3. 代码书写

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return 97;
}

我们的这个方法, 首先传出的是一个数据, 等我们将我们的plist文件传入之后, 我们再重写代码,而在以前,我们写其他的程序的时候, 就有提到过,一旦遇到数据. 我们尽量使用可以代替的变量.
例如, 在这里我们使用的是数据: 97.这个代表的是我们这个展示英雄程序, 一共有97个英雄.
但是, 一旦我们导入了plist文件, 那么我们就可以利用return self.heros.count;代替


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

     heros *hero = self.heros[indexPath.row];


     cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];

    return cell;
}

在这个方法中:

  • UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];注意的是这里面cell的状态不是先前的的default了.
    而现在这种状态是效果是, 我们的每一个cell可以显示的有: 图标`cell名\简述
  • 上面的cell的三种方法: textLable之类的, 其实都是由我们的hero这个数组提供的, 另一种说法就是: 都是由我们的plist文件提供的

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return 100;
    return 60;
}
  • 这个方法是我们设置每一个cell的高度. 这个方法可以随意设置任意一行的高度
  • self.tableView.rowHeight = 60;这一行代码也是设置每一个cell的高度, 但是这一个与上面的方法区别就是, 这个只可以将每一个cell设置为一致的高度, 不能单独设置,任意一行的高度了

至于我们的plist文件的导入 , 在这里,我并不想再说一遍了, 所以我的选择是直接上代码:

- (NSArray *)heros
{
    if (_heros == nil) {
         // 初始化
        // 1.获得plist的全路径
         NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
    
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
    
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *heroArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
             // 3.1.创建模型对象
             GJHero *hero = [GJHero heroWithDict:dict];
        
             // 3.2.添加模型对象到数组中
             [heroArray addObject:hero];
         }
    
         // 4.赋值
         _heros = heroArray;
     }
    return _heros;
}
  • 这个是在控制器里写的代码, 注意在一开始我们也要创建一个属性:数组:
    @property (nonatomic, strong) NSArray *heros;
  • 类外我们要创建一个类, 用来存放我们的数据: (类的名称就是hero)

这个是.m文件
#import "GJHero.h"

@implementation GJHero
+ (instancetype)heroWithDict:(NSDictionary *)dict
{
      return [[self alloc] initWithDict:dict];
}

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

这个是.h文件

#import <Foundation/Foundation.h>

@interface GJHero : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *intro;

+ (instancetype)heroWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,335评论 30 472
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 5,199评论 0 1
  • 系列文章: TableView优化之高度缓存功能 TableView优化之加载图片的优化逻辑 TableView优...
    老司机Wicky阅读 15,646评论 72 341
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,378评论 25 709
  • FD-03,不,圣贤再次变成了孤身一人,可他的心境却与一千年前大不相同。他不再因为孤独而感到阵阵寒意,也不再因为同...
    程序猎人阅读 3,602评论 0 1