您的位置:首页 > 产品设计 > UI/UE

重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件

2014-07-14 08:57 579 查看
重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件



为什么会需要这样子的一种方法来实现隐藏或者显示一个cell中的某些控件呢?

其实,隐藏cell中某些控件可以直接在tableView:cellForRowAtIndexPath:方法中直接实现,我们需要判断外部变量比如bool值来决定是否显示这个控件,但需要额外的代码写在tableView:cellForRowAtIndexPath:方法当中,如果我们把bool值传递给该cell让其自己判断是否显示隐藏这个控件,可读性将会大幅增加:)

效果:



源码:

YXCell.h

//
//  YXCell.h
//  SomeCell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YXCell : UITableViewCell

@property (nonatomic, strong) UIImageView   *headView;      // 头像
@property (nonatomic, assign) BOOL           showHeadView;  // 是否显示头像

@property (nonatomic, strong) UILabel       *name;
@property (nonatomic, assign) BOOL           showName;

@end


YXCell.m

//
//  YXCell.m
//  SomeCell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "YXCell.h"

@implementation YXCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// 头像
_headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 160, 100)];
[self addSubview:_headView];

// 标签
_name      = [[UILabel alloc] initWithFrame:CGRectMake(180, 10, 200, 30)];
_name.font = [UIFont fontWithName:@"HelveticaNeue-Thin"
size:20.f];
_name.textColor = [UIColor orangeColor];
[self addSubview:_name];
}
return self;
}

@synthesize showHeadView = _showHeadView;
- (void)setShowHeadView:(BOOL)showHeadView
{
_showHeadView = showHeadView;
if (_showHeadView == YES)
{
_headView.alpha = 1;
}
else
{
_headView.alpha = 0;
}
}

@synthesize showName = _showName;
- (void)setShowName:(BOOL)showName
{
_showName = showName;
if (_showName == YES)
{
_name.alpha = 1;
}
else
{
_name.alpha = 0;
}
}

@end


RootViewController.m

//
//  RootViewController.m
//  SomeCell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXCell.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView   *tableView;
@property (nonatomic, strong) NSArray       *data;

@end

@implementation RootViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// 初始化数据源
_data = @[@{@"showHeadView": [NSNumber numberWithBool:YES],
@"showName"    : [NSNumber numberWithBool:YES],
@"name"        : @"YouXianMing"},
@{@"showHeadView": [NSNumber numberWithBool:YES],
@"showName"    : [NSNumber numberWithBool:NO],
@"name"        : @"YouTianXing"},
@{@"showHeadView": [NSNumber numberWithBool:YES],
@"showName"    : [NSNumber numberWithBool:YES],
@"name"        : @"YouJin"},
@{@"showHeadView": [NSNumber numberWithBool:NO],
@"showName"    : [NSNumber numberWithBool:NO],
@"name"        : @"YouXia"},
@{@"showHeadView": [NSNumber numberWithBool:NO],
@"showName"    : [NSNumber numberWithBool:YES],
@"name"        : @"YouMeng"},
@{@"showHeadView": [NSNumber numberWithBool:YES],
@"showName"    : [NSNumber numberWithBool:YES],
@"name"        : @"YouZiLing"}];

// 初始化tableView
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
_tableView.delegate   = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reusedID = @"YXCell";
YXCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
if (cell == nil)
{
cell = [[YXCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedID];
}

// 设置头像
cell.headView.image = [UIImage imageNamed:@"back.jpg"];
cell.showHeadView   = [_data[indexPath.row][@"showHeadView"] boolValue];

// 设置文本
cell.name.text = _data[indexPath.row][@"name"];
cell.showName  = [_data[indexPath.row][@"showName"] boolValue];

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}

@end






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