標籤:style blog http color os io strong ar for
動態切換tableView中的cell的種類
為什麼要動態切換tableView中cell的種類呢?如果專案經理不出這種需求,你也就見不到這篇文章了:)
效果:
源碼:
首先,你要準備3種cell,直接繼承系統的就行了.
//// 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
分析:
用這個來標示重用吧
有一個標籤是用來切換cell類型的,以及對應的資料來源
根據切換標籤來決定初始化哪一種cell
就是這樣子實現的.
動態切換tableView中的cell的種類