点击cell的高度变化

鹏哥写的一个小demo

效果图是这样的:




代码是:

#import "ViewController.h"

#import "CustomCell.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableV;

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;

@end

static NSString *const cellID = @"cellID";

@implementation ViewController

{

    CustomCell *cell;

    NSMutableDictionary *selectedIndexes;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.tableV.backgroundColor = [UIColor lightGrayColor];


      selectedIndexes = [[NSMutableDictionary alloc] init];

     [self.tableV registerNib:[UINib nibWithNibName:NSStringFromClass([CustomCell class]) bundle:nil] forCellReuseIdentifier:cellID];

}





- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {

    NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];

    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];

}



//

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    if ([self cellIsSelected:indexPath]) {

        return 60;

    }

    return 35;

    

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    BOOL isSelected = ![self cellIsSelected:indexPath];

    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];

    [selectedIndexes setObject:selectedIndex forKey:indexPath];

    

    [self.tableV beginUpdates];

    [self.tableV endUpdates];

}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    cell.button.tag = 100+indexPath.row;

    cell.button.backgroundColor = [UIColor redColor];

    [cell.button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    return cell;

}

-(void)btnClick:(UIButton *)sender{

    

 

}

@end








评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值