tableview实现单个cell的展开和收起

此篇主要介绍采用替换cell的方式来实现cell的收起和展开效果,下面是效果图,分为只能展开一个cell和都能展开两种



代码实现

#import "replaceVC.h"
#import "TitleCell.h"
#import "DetailCell.h"

@interface replaceVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *replaceTable;

@property (nonatomic,strong) NSMutableArray *dataArr;

@property (nonatomic,strong) NSIndexPath *selectedIndexPath;

@property (nonatomic,strong) NSIndexPath *preSelectedIndexPath;

@property (nonatomic,strong) NSMutableArray *expandArr;

@end

@implementation replaceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.replaceTable];
}

#pragma mark UITableViewDelegate,UITableViewDataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:[self.dataArr objectAtIndex:indexPath.row]];
    /*
    //整个表只展示一个cell,其余的收起
    if (self.selectedIndexPath != indexPath) {
        TitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCell"];
        if (!cell) {
            cell = [[TitleCell alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        }
        cell.dataDic = dic;
        return cell;
    }
    
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    if (!cell) {
        cell = [[DetailCell alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
    cell.dataDic = dic;
    return cell;
    */
    
    NSInteger status = [[self.expandArr objectAtIndex:indexPath.row] integerValue];
    
    NSLog(@"++++++++++++:%ld",(long)status);
    
    if (0 == status) {
        TitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCell"];
        if (!cell) {
            cell = [[TitleCell alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        }
        cell.dataDic = dic;
        return cell;
    }
    
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    if (!cell) {
        cell = [[DetailCell alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
    cell.dataDic = dic;
    return cell;
     
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    //整个表只展示一个cell,其余的收起
    self.preSelectedIndexPath = self.selectedIndexPath;
    self.selectedIndexPath = indexPath;
    
    [tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    if (self.preSelectedIndexPath) {
        [tableView reloadRowsAtIndexPaths:@[self.preSelectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
//    [tableView scrollToRowAtIndexPath:self.selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
     */
    
    
    [self changeExpandArrAtIndexPath:indexPath];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     //整个表只展示一个cell,其余的收起
    if(self.selectedIndexPath != indexPath)
    {
        return 50;
    }
    return 200;
     */
    
    
    NSInteger status = [[self.expandArr objectAtIndex:indexPath.row] integerValue];
    
    if(0 == status)
    {
        return 50;
    }
    return 200;
    
}

#pragma mark pivate
-(void)changeExpandArrAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger status = [[self.expandArr objectAtIndex:indexPath.row] integerValue];
    if (0 == status) {
        [self.expandArr replaceObjectAtIndex:indexPath.row withObject:@1];
    }
    else
    {
        [self.expandArr replaceObjectAtIndex:indexPath.row withObject:@0];
    }
}

#pragma mark 懒加载
-(UITableView *)replaceTable
{
    if (!_replaceTable) {
        _replaceTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _replaceTable.delegate = self;
        _replaceTable.dataSource = self;
        [_replaceTable registerNib:[UINib nibWithNibName:@"TitleCell" bundle:nil] forCellReuseIdentifier:@"TitleCell"];
        [_replaceTable registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:@"DetailCell"];
    }
    return _replaceTable;
}

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"replaceData.plist" ofType:nil]];
    }
    return _dataArr;
}

-(NSMutableArray *)expandArr
{
    if (!_expandArr) {
        _expandArr = [[NSMutableArray alloc]init];
        for (NSDictionary *dic in self.dataArr) {
            [_expandArr addObject:@0];
        }
    }
    return _expandArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

cell采用的是xib,设置如下图



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值