您的位置:首页 > 其它

点击cell的高度变化

2016-11-30 17:57 399 查看
鹏哥写的一个小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









内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息