------------- ViewController.m -------------
#import "ViewController.h"
#import "CZHero.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic,strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (NSArray *)heros
{
if (_heros == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray)
{
CZHero *hero = [CZHero heroWithDict:dict];
[tmpArray addObject:hero];
}
_heros = tmpArray;
}
return _heros;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"hero";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
CZHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CZHero *hero = self.heros[indexPath.row];
NSString *name = hero.name;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"要修改的英雄名称" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textF = [alertView textFieldAtIndex:0];
textF.text = name;
[alertView show];
alertView.tag = indexPath.row;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) return;
NSString *newName = [alertView textFieldAtIndex:0].text;
NSUInteger row = alertView.tag;
CZHero *hero = self.heros[row];
hero.name = newName;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationMiddle];
}
@end
------------- CGHero.h -------------
#import <Foundation/Foundation.h>
@interface CZHero : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *intro;
@property (nonatomic,copy) NSString *name;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)heroWithDict:(NSDictionary *)dict;
@end
------------- CGHero.m -------------
#import "CZHero.h"
@implementation CZHero
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)heroWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end