之前没有用到过这块,但是今天看到,就试了试,但是发现,网上的有的方法不能多个cell同时展开,只能一个一个的展开。
我就尝试用用数组记录展开的标记的方法,功能实现了,
直接上代码:
// // ViewController.m // 折叠tableview-Test // // Created by abc on 16/7/26. // Copyright © 2016年 LiuWenqiang. All rights reserved. // #import "ViewController.h" #define ALLWIDTH [UIScreen mainScreen].bounds.size.width #define ALLHEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> { UITableView*tableview; NSArray *dataArr; NSMutableArray *isOpenArr; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, ALLWIDTH, ALLHEIGHT-40)]; tableview.delegate =self; tableview.dataSource = self; tableview.tableFooterView = [[UIView alloc]init]; [self.view addSubview:tableview]; dataArr = [NSArray array]; dataArr = @[@"呵呵",@"哈哈",@"嗯嗯",@"呃呃",@"很好"]; isOpenArr = [NSMutableArray array]; for (int i =0; i< dataArr.count; i++) { [isOpenArr addObject:@"0"]; } } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 5; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if([isOpenArr[section] isEqualToString:@"1"]) { return dataArr.count; }else{ return 0; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellstr = @"cell"; UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr]; if (!cell) { cell = [tableview dequeueReusableCellWithIdentifier:cellstr forIndexPath:indexPath]; } cell.textLabel.text = dataArr[indexPath.row]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view =[[UIView alloc]init]; UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,ALLWIDTH, 50)]; lable.text = [NSString stringWithFormat:@"------------第%ld组---------",(long)section]; lable.textColor = [UIColor redColor]; [view addSubview:lable]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topgesture:)]; [view addGestureRecognizer:tap]; view.tag = section; return view; } //在组头上添加点击手势 -(void)topgesture:(UITapGestureRecognizer*)tap { NSInteger index = tap.view.tag; if ([isOpenArr[index] isEqualToString:@"1"]) { [isOpenArr replaceObjectAtIndex:index withObject:@"0"]; }else{ [isOpenArr replaceObjectAtIndex:index withObject:@"1"]; } [tableview reloadData]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableview deselectRowAtIndexPath:indexPath animated:YES]; NSLog(@"------================--%ld-----",indexPath.row); } @end
自己最近几天正在学习swifit,于是就试着用swift 写了一遍,
// // ViewController.swift // 测试--Swift--Test // // Created by abc on 16/7/26. // Copyright © 2016年 LiuWenqiang. All rights reserved. // import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let ALLwidth = UIScreen.mainScreen().bounds.width let ALLheight = UIScreen.mainScreen().bounds.height var tableview:UITableView = UITableView() var DataArr: NSMutableArray = NSMutableArray() var selectindex:NSMutableArray = NSMutableArray() override func viewDidLoad() { super.viewDidLoad() tableview.frame = CGRectMake(0, 50, ALLwidth, ALLheight) tableview.delegate = self tableview.dataSource = self self.view.addSubview(tableview) DataArr = ["第一行","第二行","第三行","第四行"] for _ in DataArr { selectindex.addObject("0") } } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 4 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if selectindex[section] .isEqual("1") { return DataArr.count }else{ return 0 } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let indenfer = "cell" let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: indenfer) cell.textLabel?.text = DataArr[indexPath.row] as? String return cell } func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50 } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { let str:String = "-----------" + "(section)" + "-------------" return str } func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerview:UIView = UIView() headerview.frame = CGRectMake(0, 0, ALLwidth,50) headerview.backgroundColor = UIColor.grayColor() let lable:UILabel = UILabel() lable.frame = headerview.bounds lable.text = "-----------" + "第(section)组" + "-------------" lable.textColor = UIColor.redColor() headerview.addSubview(lable) let tap = UITapGestureRecognizer.init(target: self, action:#selector(ViewController.clicktap(_:))) headerview.tag = section headerview.addGestureRecognizer(tap) return headerview } func clicktap(tap:UITapGestureRecognizer){ print("=====(tap.view?.tag)") let index:Int = tap.view!.tag if selectindex[index] .isEqual("1"){ selectindex.replaceObjectAtIndex(index, withObject: "0") }else{ selectindex.replaceObjectAtIndex(index, withObject: "1") } tableview .reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }