之前没有用到过这块,但是今天看到,就试了试,但是发现,网上的有的方法不能多个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(, , ALLWIDTH, ALLHEIGHT-)];
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 =; i< dataArr.count; i++) { [isOpenArr addObject:@""];
} }
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([isOpenArr[section] isEqualToString:@""])
{
return dataArr.count;
}else{
return ;
}
}
-(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 ;
} -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view =[[UIView alloc]init];
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(, ,ALLWIDTH, )];
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:@""]) { [isOpenArr replaceObjectAtIndex:index withObject:@""]; }else{
[isOpenArr replaceObjectAtIndex:index withObject:@""]; }
[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(, , ALLwidth, ALLheight)
tableview.delegate = self
tableview.dataSource = self
self.view.addSubview(tableview) DataArr = ["第一行","第二行","第三行","第四行"] for _ in DataArr { selectindex.addObject("")
} } func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return
} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if selectindex[section] .isEqual("") { return DataArr.count }else{ return
} }
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
}
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(, , ALLwidth,)
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(""){ selectindex.replaceObjectAtIndex(index, withObject: "") }else{ selectindex.replaceObjectAtIndex(index, withObject: "")
} tableview .reloadData() } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }