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

来源:互联网 发布:mysql minute函数 编辑:程序博客网 时间:2024/06/08 08:15

//

//  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 =[superinit]) {

        [selfsetValuesForKeysWithDictionary:dict];

    }

    return self;

}


+(instancetype)heroWithDict:(NSDictionary *)dict {

   return [[selfalloc] 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) IBOutletUITableView *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 {

    [superviewDidLoad];

   //1.首先设置数据源

   self.tableView.dataSource =self;//控制器就是数据源

   self.tableView.delegate =self;

}


#pragma mark - 懒加载

- (NSArray *)heros {

   if (_heros ==nil) {

        // 1.获得全路径

       NSString *fullPath = [[NSBundlemainBundle] pathForResource:@"heros.plist"ofType:nil];

        // 2.根据全路径获得数据---设置字典

       NSArray *dictArray = [NSArrayarrayWithContentsOfFile:fullPath];

        // 3.字典转模型

        // 3.1先设置模型(为可变数组),并设置模型中要放入的对象个数

       NSMutableArray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];

        // 3.2遍历模型,取出每个模型中的对象

       for (NSDictionary *dictin dictArray) {

            // 3.3字典中的每个对象都为WBHero类型,取出

           WBHero *hero = [WBHeroheroWithDict:dict];

            [modelsaddObject: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 = [tableViewdequeueReusableCellWithIdentifier:ID];

   if (cell == nil) {

       NSLog(@"重用前的实例化,%ld",indexPath.row);

        

        // 实例化新的单元格

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier: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 = [UIImageimageNamed: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;




/**

typedefNS_ENUM(NSInteger, UIButtonType) {

    UIButtonTypeCustom =0,                         // no button type

    UIButtonTypeSystemNS_ENUM_AVAILABLE_IOS(7_0), // standard system button


    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

    

    UIButtonTypeRoundedRect = UIButtonTypeSystem,  // Deprecated, use UIButtonTypeSystem instead

};

*/


cell.accessoryView = [UIButtonbuttonWithType:UIButtonTypeContactAdd];



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



//UISwitch

   UISwitch *switcher = [[UISwitchalloc] init];

    // 添加监听方法

    [switcher addTarget:selfaction:@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


0 0
原创粉丝点击