tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift

之前没有用到过这块,但是今天看到,就试了试,但是发现,网上的有的方法不能多个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.
    }


}

 

转载于:https://www.cnblogs.com/liuwenqiang/p/5707109.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值