#import <Foundation/Foundation.h>
@interface TXHero : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *intro;
+ (instancetype)heroWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
==================
#import "TXHero.h"
@implementation TXHero
+ (instancetype)heroWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if(self = [super init]){
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end
===================
#import "TXViewController.h"
#import "TXHero.h"
@interface TXViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end
@implementation TXViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableView.rowHeight = 60;
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
// bellow is UITableViewDelegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TXHero *hero = self.heros[indexPath.row];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your selected is:" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert textFieldAtIndex:0].text = hero.name;
alert.tag = indexPath.row;
[alert show];
}
// bellow is UIAlertViewDelegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0) return;
int row = alertView.tag;
TXHero *hero = self.heros[row];
hero.name = [alertView textFieldAtIndex:0].text;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
// bellow is UITableViewDatasource method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
// bellow is UITableViewDatasource method
- (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];
}
TXHero *hero = self.heros[indexPath.row];
cell.imageView.image = [UIImage imageNamed:hero.icon];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
return cell;
}
- (NSArray *)heros
{
if(_heros == nil)
{
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"]];
NSMutableArray *heroArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
TXHero *hero = [TXHero heroWithDict:dict];
[heroArray addObject:hero];
}
_heros = heroArray;
}
return _heros;
}
@end