動態展開tableView的cell[2]ios
http://code4app.com/ios/%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0cell/53845f8a933bf0740a8b458aapp
這份代碼也是參考別人而寫的-_-!性能
效果:atom
其實呢,這份代碼本人是不推薦的,很難維護,由於他的原理就是添加刪除cell,會有這複雜的刪除添加邏輯.spa
源碼:code
// // RootViewController.m // InsertTableViewCell // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" @interface RootViewController ()<UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化數據源 NSDictionary *dic = @{@"Cell" : @"MainCell", @"isAttached" : @(NO)}; NSArray *array = @[dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic, dic]; _dataArray = [NSMutableArray arrayWithArray:array]; // 初始化tableView _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; } // ==================================== #pragma mark - #pragma mark dataSource // ==================================== - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataArray.count; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // 重繪重用會一直走這個方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"重用"); if ([_dataArray[indexPath.row][@"Cell"] isEqualToString:@"MainCell"]) { static NSString *reusedID = @"YouXianMing"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedID]; } cell.textLabel.text = @"YouXianMing"; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor = [UIColor whiteColor]; return cell; } if ([_dataArray[indexPath.row][@"Cell"] isEqualToString:@"AttachedCell"]) { static NSString *reusedID = @"AttachedCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedID]; } cell.textLabel.text = @"NoZuoNoDie"; cell.textLabel.textColor = [UIColor redColor]; cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:12.f]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor = [UIColor whiteColor]; return cell; } return nil; } // ==================================== #pragma mark - #pragma mark delegate // ==================================== - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"計算高度"); if ([self.dataArray[indexPath.row][@"Cell"] isEqualToString:@"MainCell"]) { return 50; } else { return 30; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"選擇了第 %d 行", indexPath.row); [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSIndexPath *path = nil; if ([self.dataArray[indexPath.row][@"Cell"] isEqualToString:@"MainCell"]) { // 若是點擊的時MainCell,則添加一行 path = [NSIndexPath indexPathForItem:(indexPath.row + 1) inSection:indexPath.section]; } else { path = indexPath; } if ([self.dataArray[indexPath.row][@"isAttached"] boolValue] == YES) { // 關閉附加cell self.dataArray[path.row - 1] = @{@"Cell" : @"MainCell", @"isAttached" : @(NO)}; [self.dataArray removeObjectAtIndex:path.row]; [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; } else { // 打開附加cell self.dataArray[path.row - 1] = @{@"Cell" : @"MainCell", @"isAttached" : @(YES)}; NSDictionary * addDic = @{@"Cell" : @"AttachedCell", @"isAttached" : @(YES)}; [self.dataArray insertObject:addDic atIndex:path.row]; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; } } @end
核心的地方-blog
執行下面的操做(增長或者刪除修改等):rem
會致使強制計算全部cell的高度:源碼
這一點沒有處理好是會影響性能的,注意哦.it
沒有更多的地方須要說的了......