標籤: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