点击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
效果图是这样的:
代码是:
#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
相关文章推荐
- 点击单个cell高度变化的动画效果
- 点击单个cell高度变化的动画效果
- cell的高度随点击变化
- 点击单个cell高度变化的动画效果
- tableViewCell 内部按钮点击事件 和 cell 高度变化操作
- tableview 高度适配内容(高度随着cell行数变化而动态变化)及(监听contentsize变化)
- iOS - UITableView cell中添加展开关闭的功能(cell高度动态变化)
- cell高度随文字变化
- 点击cell动态修改高度动画
- tableview group cell xib创建,点击刷新高度+2的问题
- 回到顶部,当页面下拉到一定高度时,就会出现回到顶部的按钮,点击回到顶部之后,会有一个速度的变化回滚到顶部,如果正在回到顶部时,鼠标的滚轮转动了就会停止回到顶部
- 【学习笔记】tableview 文字设置 缩进,头尾标题 间距,cell高度 点击 分割线
- TableView 点击cell,改变选中cell的高度
- 模仿动态增加或者删除cell并自动增加变化高度
- 计算cell的高度,让cell的高度随填充的文本字数变化而变化
- iOS tableViewCell 点击后没有任何变化
- Swift - 实现点击cell动态修改高度
- Swift之点击UITableView单元格动态改变cell高度
- iOS 中tableview的headerView高度变化是覆盖cell的问题
- 根据展示文字自适应 cell 高度,实现点击cell的伸缩扩展