動態切換tableView中的cell的種類atom
爲何要動態切換tableView中cell的種類呢?若是項目經理不出這種需求,你也就見不到這篇文章了:)spa
效果:3d
源碼:code
首先,你要準備3種cell,直接繼承系統的就好了.orm
// // RootViewController.m // ChangeCell // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import "YellowCell.h" #import "RedCell.h" #import "TitleCell.h" // ------------------------------ static NSString *CELL[] = { @"TitleCellFlag", @"RedCellFlag", @"YellowCellFlag", }; typedef enum : NSUInteger { Title, Red, Yellow, } CellType; // ------------------------------ @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSString *changeFlag; // 切換標籤 @property (nonatomic, strong) NSArray *dataArray; // 數據源 @property (nonatomic, strong) NSArray *redData; // 紅色cell數據 @property (nonatomic, strong) NSArray *yellowData; // 黃色cell數據 @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化TableView _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; // 紅色cell數據 _redData = @[@"", @"", @"", @""]; // 黃色cell數據 _yellowData = @[@"", @"", @"", @"", @"", @"", @""]; // 數據源 _dataArray = _redData; // 類型 _changeFlag = CELL[Red]; // 4秒鐘以後切換cell [self performSelector:@selector(runSelector:) withObject:nil afterDelay:9]; } - (void)runSelector:(id)sender { // 數據源 _dataArray = _yellowData; // 類型 _changeFlag = CELL[Yellow]; // 從新加載數據 [_tableView reloadData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_dataArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; if (indexPath.row == 0) // 第一格cell { cell = [[TitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL[Title]]; cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18]; cell.textLabel.text = @"YouXianMing"; cell.textLabel.textColor = [UIColor redColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } if (indexPath.row != 0) // 其餘cell { if ([_changeFlag isEqualToString:CELL[Red]]) { cell = [[RedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL[Title]]; cell.backgroundColor = [UIColor redColor]; // 紅色 cell.selectionStyle = UITableViewCellSelectionStyleNone; } if ([_changeFlag isEqualToString:CELL[Yellow]]) { cell = [[YellowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL[Title]]; cell.backgroundColor = [UIColor yellowColor]; // 黃色 cell.selectionStyle = UITableViewCellSelectionStyleNone; } } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { return 70; } if ([_changeFlag isEqualToString:CELL[Red]]) { return 100; } if ([_changeFlag isEqualToString:CELL[Yellow]]) { return 200; } return 0; } @end
分析:blog
用這個來標示重用吧繼承
有一個標籤是用來切換cell類型的,以及對應的數據源源碼
根據切換標籤來決定初始化哪種cellit
就是這樣子實現的.io