现在的位置: 首页 > 综合 > 正文

lol英雄列表显示

2018年05月26日 ⁄ 综合 ⁄ 共 4397字 ⁄ 字号 评论关闭

通过这个例子可以对UITableView有一个基本的了解

并且在加载Cell时使用了内存优化,引用了对内存池中对象的调用,提高性能

在修改数据的时候,使用的是UIAlertView,使用了更新优化,只是让被修改的一项cell刷新操作,其他的项不变

基本思路还是:

通过初始化数据源

字典转模型

赋值数据

initCustom.h:这个文件是把h文件和m文件中公共的一段代码通过这种带有参数的宏定义来实现

#ifndef _2_0527UITableView____initCustom_h
#define _2_0527UITableView____initCustom_h
#define init(name)\
- (instancetype)initWithDict:(NSDictionary *)dict;\
+ (instancetype)name##WithDict:(NSDictionary *)dict;

#define initWithDict(name)\
- (instancetype)initWithDict:(NSDictionary *)dict{\
    self = [super init];\
    if(self){\
        [self setValuesForKeysWithDictionary:dict];\
    }\
    return self;\
}\
+ (instancetype)name##WithDict:(NSDictionary *)dict{\
    return [[self alloc] initWithDict:dict];\
}
#endif

调用:

CHHeros.m:

#import "CHHeros.h"

@implementation CHHeros
/**
 *  实现initWithDict和herosWithDict
 */
initWithDict(heros);
@end

CHHeros.h:

#import <UIKit/UIKit.h>
#import "initCustom.h"
@interface CHHeros : NSObject
@property (copy,nonatomic) NSString *icon;
@property (copy,nonatomic) NSString *name;
@property (copy,nonatomic) NSString *intro;
/**
 *  对外提供初始化的herosWithDict类方法
 */
init(heros)
@end

CHViewController.m:这里实现了三个协议的方法

#import "CHViewController.h"
#import "CHHeros.h"
@interface CHViewController () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (strong,nonatomic) NSArray *herosArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation CHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor redColor];
    //设置头部View尾部View
    self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];

}
//懒加载模型数组数据
- (NSArray *)herosArray{
    if(!_herosArray){
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrayM = [[NSMutableArray alloc] initWithCapacity:array.count];
        for(NSDictionary *dict in array){
            CHHeros *heros = [CHHeros herosWithDict:dict];
            [arrayM addObject:heros];
        }
        _herosArray = [arrayM copy];
    }
    return _herosArray;
}
//告诉TableView一共有几组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//告诉TableView每组有多少数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.herosArray.count;
}

//为每一个cell设置数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //内存优化:
    //先去内存中找是否有hero标记的缓存对象,如果有,则直接返回,并赋值,如果没有,则新建一个对象,赋值并打上hero标签
    static NSString * identify = @"hero";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
//        NSLog(@"创建了一个新对象");
    }
    //indexPath.section 是不对的
    CHHeros *heros = self.herosArray[indexPath.row];//这里返回的是第几行
    cell.imageView.image=[UIImage imageNamed:heros.icon];
    cell.textLabel.text = heros.name;
    cell.detailTextLabel.text = heros.intro;
    //设置cell的辅助视图
//    cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    cell.accessoryView = [[UISwitch alloc] init];
    //设置cell的背景颜色
    cell.backgroundColor = [UIColor grayColor];
    // 设置选中状态的背景
//    UIView *backView = [[UIView alloc] init];
//    backView.backgroundColor = [UIColor darkGrayColor];
//    cell.selectedBackgroundView = backView;
    // 设置默认状态的背景---即backgroundView,因为它会覆盖backgroundColor
    cell.backgroundColor = [UIColor whiteColor];
//    NSLog(@"heros.name:%@",heros.name);
    return cell;
}
#pragma mark - 点击每一行的Cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CHHeros * heros = self.herosArray[indexPath.row];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"修改姓名" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField *text = [alert textFieldAtIndex:0];
    text.text = heros.name;
    [alert show];
    alert.tag = indexPath.row;
    
}
#pragma mark - 点击UITableView时出发这个事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//    NSLog(@"%d",buttonIndex);
    if(0 == buttonIndex) return;
    UITextField *textField = [alertView textFieldAtIndex:0];
    NSString *text = textField.text;
    int row = alertView.tag;
    CHHeros *hero = _herosArray[row];
    hero.name = text;
    //修改cell中的text值后,立即更新这个cell
    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark - 控制状态栏是否显示
- (BOOL)prefersStatusBarHidden{
    return YES;
}


@end

==============

在设置id标示符的时候,为了提高性能,要把标示设置为static

【上篇】
【下篇】

抱歉!评论已关闭.