码迷,mamicode.com
首页 > 其他好文 > 详细

37.小项目:英雄列表 版本2.0

时间:2015-08-10 07:06:39      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

------------- 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

 

37.小项目:英雄列表 版本2.0

标签:

原文地址:http://www.cnblogs.com/lixiang2015/p/4716886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!