標籤:style blog http color 使用 io strong for
將一種cell當做幾種cell使用
將一種cell當做幾種cell用是有著一些意義的,比如,有時候,不同的cell之間差異很小,如果再派生一個cell出來,就會顯得很麻煩,這時候,將這個cell當做幾個cell用才有市場的說:)
效果:
源碼:
ModelCell.h 與 ModelCell.m
//// ModelCell.h// Cells//// Copyright (c) 2014年 Y.X. All rights reserved.//#import <UIKit/UIKit.h>@interface ModelCell : UITableViewCell@property (nonatomic, assign) BOOL show1;@property (nonatomic, assign) BOOL show2;@property (nonatomic, assign) BOOL show3;@end
RootViewController.m
//// RootViewController.m// Cells//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "ModelCell.h"@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSArray *dataSource;@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; // tableView _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:_tableView];}#pragma mark - UITableView‘s dataSource & delegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 100;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reusedFlag = @"YouXianMing"; ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag]; if (cell == nil) { cell = [[ModelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedFlag]; } cell.show1 = YES; cell.show2 = YES; cell.show3 = YES; return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 100.f;}@end
核心的地方:
根據屬性的setter方法動態隱藏cell中的某些控制項
然後在配置cell的時候動態設定要顯示的控制項
just so easy :)