您的位置:首页 > 编程语言

英雄展示代码进一步完善(包括可重用单元格)

2015-09-30 15:02 267 查看
//

//  WBHero.h

//  0929UITableView练习

//

//  Created by weibiao on 15-9-29.

//  Copyright (c) 2015年 weibiao. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface WBHero :
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

//

//  WBHero.m

//  0929UITableView练习

//

//  Created by weibiao on 15-9-29.

//  Copyright (c) 2015年 weibiao. All rights reserved.

//

#import "WBHero.h"

@implementation WBHero

- (instancetype)initWithDict:(NSDictionary *)dict {
   
if (self =[super
init]) {

        [self
setValuesForKeysWithDictionary:dict];
    }

    return
self;
}

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

@end

//

//  ViewController.m

//  0929UITableView练习

//

//  Created by weibiao on 15-9-29.

//  Copyright (c) 2015年 weibiao. All rights reserved.

//

#import "ViewController.h"

#import "WBHero.h"

@interface
ViewController ()<UITableViewDataSource,UITableViewDelegate>//2.设置遵守的协议

@property (weak,
nonatomic) IBOutlet
UITableView *tableView;

@property (strong,
nonatomic) NSArray *heros;

@end

@implementation ViewController//遵守的协议必须实现UITableViewDataSource文档中required的方法,否则会有警告

/**

 @required

 

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

 

 // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

 // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

 

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

 

 @optional

*/

- (void)viewDidLoad {

    [super
viewDidLoad];

   //1.首先设置数据源
   
self.tableView.dataSource =
self;//控制器就是数据源
   
self.tableView.delegate =
self;
}

#pragma mark - 懒加载
- (NSArray *)heros {
   
if (_heros ==
nil) {

        // 1.获得全路径
       
NSString *fullPath = [[NSBundle
mainBundle] pathForResource:@"heros.plist"
ofType:nil];

        // 2.根据全路径获得数据---设置字典
       
NSArray *dictArray = [NSArray
arrayWithContentsOfFile:fullPath];

        // 3.字典转模型

        // 3.1先设置模型(为可变数组),并设置模型中要放入的对象个数
       
NSMutableArray *models = [NSMutableArray
arrayWithCapacity:dictArray.count];

        // 3.2遍历模型,取出每个模型中的对象
       
for (NSDictionary *dict
in dictArray) {

            // 3.3字典中的每个对象都为WBHero类型,取出
           
WBHero *hero = [WBHero
heroWithDict:dict];
            [models
addObject:hero];//把遍历的对象放入模型
        }
       
_heros = [models copy];
    }
   
return _heros;
}

#pragma mark - UITableViewDataSource

//3.设置返回的组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
return 1;
}

//4.设置返回的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return
self.heros.count;
}

// 5.设置可重用的cell,此方法使用频率非常高
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {

   

//表格明细

//    NSLog(@"实例化,表格明细,%ld",indexPath.row);

    

    // 5.0可重用表格标识符

    //
静态变量,能够保证系统只为变量分配一次内存空间,静态变量一旦创建就不会被释放,直至应用程序被销毁,才释放
   
static NSString *ID =
@"Cell";

    //5.1取缓存池中可重用的单元格

    UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
   
if (cell == nil) {
       
NSLog(@"重用前的实例化,%ld",indexPath.row);

        

        //
实例化新的单元格

        cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:ID];
    }

    //代码运行至此一定有了单元格

    

    //5.1创建cell

//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    // 5.2取得英雄对象
   
WBHero *model = self.heros[indexPath.row];

    // 5.3设置数据

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



 

/**

     typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

     UITableViewCellAccessoryNone,                   // 

     默认情况下:don't show any accessory view

     UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track  

     UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks  箭头

     UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track 对号

     UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks

     };

     */

   

    //
设置右边的箭头,提示用户当前行能够点击,通常会跳转到下一个界面

    cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;



// 设置对号,通常提示用户设置完毕,用的较少

    cell.accessoryType =
UITableViewCellAccessoryCheckmark;



//按钮

cell.accessoryType =
UITableViewCellAccessoryDetailButton;



//按钮+箭头,有各自的功能

cell.accessoryType =
UITableViewCellAccessoryDetailDisclosureButton;



/**

typedef
NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom =
0,                        
// no button type
    UIButtonTypeSystem
NS_ENUM_AVAILABLE_IOS(7_0), 
// standard system button

    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,
    UIButtonTypeContactAdd,

    

    UIButtonTypeRoundedRect = UIButtonTypeSystem,  
// Deprecated, use UIButtonTypeSystem instead
};
*/

cell.accessoryView = [UIButton
buttonWithType:UIButtonTypeContactAdd];



cell.accessoryView = [UIButton
buttonWithType:UIButtonTypeCustom];//此时不会有显示



//UISwitch
   
UISwitch *switcher = [[UISwitch
alloc] init];

    //
添加监听方法

    [switcher addTarget:self
action:@selector(switcherChanged:)
forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = switcher;



    // 5.4返回cell
   
return cell;
}

//switcherChanged监听方法
-(void)switcherChanged:(UISwitch *)sender {
   
NSLog(@"%s,%@",__func__,sender);
}

// 隐藏状态栏
-(BOOL)prefersStatusBarHidden {

    return
YES;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: