重寫UITableViewCell子類中屬性的setter方法來實現隱藏或顯示該cell中的某些控制項

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   資料   

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

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.