195,UITableView实例,展现其属性或方法

来源:互联网 发布:公文的阅知范围 编辑:程序博客网 时间:2024/05/17 03:51


HMHero.h:

#import <Foundation/Foundation.h>


@interface HMHero : NSObject

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *icon;

@property (nonatomic,copy) NSString *intro;


- (instancetype)initWithDict:(NSDictionary *)dict;

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


+ (NSArray *)heros;


@end


HMHero.m:

#import "HMHero.h"


@implementation HMHero


- (instancetype)initWithDict:(NSDictionary *)dict

{

    self = [superinit];

    if (self) {

        [selfsetValuesForKeysWithDictionary:dict];

    }

    return self;

}


+ (instancetype)heroWithDict:(NSDictionary *)dict

{

    return [[selfalloc] initWithDict:dict];

}


+ (NSArray *)heros

{

    NSArray *array = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"heros.plist"ofType:nil]];

    

    NSMutableArray *arrayM = [NSMutableArrayarray];

    for (NSDictionary *dictin array) {

        [arrayM addObject:[selfheroWithDict:dict]];

    }

    

    return arrayM;

}


@end


HMViewController.m:


#import "HMViewController.h"

#import "HMHero.h"


@interface HMViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSArray *heros;

@end


@implementation HMViewController


- (NSArray *)heros

{

    if (_heros ==nil) _heros = [HMHeroheros];

    return _heros;

}


/**

 UITableViewStylePlain,     // 平板的格式

 UITableViewStyleGrouped    // 分组的格式

 */

- (UITableView *)tableView

{

    

    if (_tableView ==nil) {

        // 表格控件在创建时必须指定样式,只能使用以下实例化方法

        _tableView = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

        

        _tableView.dataSource =self;

        _tableView.delegate =self;

        

        [self.viewaddSubview:_tableView];

    }

    return_tableView;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    [selftableView];

    

    // 设置行高

    self.tableView.rowHeight =80;

}


#pragma mark - 数据源方法

// 每个分组中的数据总数

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

{

    NSLog(@"每个分组的数据总数 %d",self.heros.count);

    

    return self.heros.count;

}


/**

 UITableViewCellStyleDefault,   默认类型 标题+可选图像

 UITableViewCellStyleValue1,    标题+明细+图像

 UITableViewCellStyleValue2,    不显示图像,标题+明细

 UITableViewCellStyleSubtitle   标题+明细+图像

 */

// 告诉表格每个单元格的明细信息

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

{

    NSLog(@"表格行明细 %d", indexPath.row);

    

    UITableViewCell *cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

    

    // 取出英雄对象

    HMHero *hero = self.heros[indexPath.row];

    

    // 标题

    cell.textLabel.text = hero.name;

    // 明细信息

    cell.detailTextLabel.text = hero.intro;

    // 图像

    cell.imageView.image = [UIImageimageNamed:hero.icon];

    

    // 设置右边的箭头

    // 1> UITableViewCellAccessoryDisclosureIndicator箭头,可以"提示"用户,当前行是可以点击的,通常选中行,会跳到新的页面

    // 2> UITableViewCellAccessoryCheckmark 对号,通常提示用户该行数据设置完毕,使用的比较少

    // 3> UITableViewCellAccessoryDetailButton按钮,通常点击按钮可以做独立的操作,例如alertView

    //    点击按钮并不会选中行

    // 4> UITableViewCellAccessoryDetailDisclosureButton按钮+箭头,各自操作

//    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    

    // 指定右侧的自定义视图

    /**

     通常accessoryType提供的类型不能满足时,才会使用自定义控件

     

     但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中!!!

     

     自定义控件的事件触发,同样不会影响表格行的选中!

     */

    UISwitch *switcher = [[UISwitchalloc] init];

    // 添加监听方法

    [switcher addTarget:selfaction:@selector(switchChanged:)forControlEvents:UIControlEventValueChanged];

    

    cell.accessoryView = switcher;

    

    return cell;

}


- (void)switchChanged:(UISwitch *)sender

{

    NSLog(@"%s %@", __func__, sender);

}


#pragma mark - 代理方法

// accessoryType为按钮时,点击右侧按钮的监听方法

// 此方法不会触发行选中,跟行选中各自独立

// 只是为accessoryType服务,对自定义控件不响应

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%s %@", __func__, indexPath);

}


// 取消选中某一行,极少用,极容易出错!

// didDeselectRowAtIndexPath

// didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%s %@", __func__, indexPath);

}


// 选中了某一行,有箭头的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%s %@", __func__, indexPath);

}


/**

 代理方法的优先级比rowHeight优先级高

 

 应用场景:很多应用程序,每一行的高度是不一样的,例如:新浪微博

 

 表格工作观察的小结

 

 1> 要知道总共有多少数据

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

 

 2> 计算每一行的行高

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

 

 问题:在此方法执行时,cell被实例化了吗?

 方法的作用是什么?

 

 scrollView需要指定contentSize才能够滚动,如果没有实现方法,行高默认是44

 

 需要知道每一行的高度,才能够准确的计算出contentSize

 

 知道每一行的高度后,自然知道每一个屏幕应该显示多少行,表格明细方法的执行次数就知道了

 

 3> 表格明细

 调用屏幕显示所需要的行数,懒加载,只有要显示的表格行,才会被实例化!

 

 小的结论:

 

 *  tableView.rowHeight   效率高,适用于所有的表格行高度一致

 *  代理方法指定行高:         效率差,适合于每一个行的行高不一样,能够让表格更加的灵活

 

 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

//    NSLog(@"行高 %d", indexPath.row);

//    

    return (indexPath.row %2) ? 60 : 44;

    

    // 以下代码可以使用rowHeight属性替换!

//    return 60;

}


@end



0 0