点击UITableView的cell展开收缩
2015-05-27 13:05
381 查看
在项目中有个需求,点击表视图的单元格展开,再点击另外一个单元格或者本身又收缩,经过一段时间尝试,实现了该功能,现在记录分享总结下。
首先要理解UITableView代理方法调用的先后顺序。
当初始化UITableView后,代理回调顺序如下
1://返回cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2://返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
3://请求数据元代理为tableView插入需要的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4://监听点击的cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
需要声明一个全局BOOL变量isOpen,记录当前cell的状态,声明一个NSIndexPath类型selectedIndex,记录选择的cell的row。
在heightForRowAtIndexPath代理里面实现//选中状态返回的高度
if
(indexPath.row == selectedIndex.row && selectedIndex != nil ) {
if
(isOpen == YES) {
//cell上的label高度自适应
CGSize
size = [textStr sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(290, 1000) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat
f = size.height;
if
(indexPath.row == [self.dataArr count]-1){
return
153.8+(f - 21);
}
return
155+(f - 21);
}else{
return
67;
}
}
同样在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里实现一样的条件
if
(indexPath.row == selectedIndex.row && selectedIndex != nil) {
//如果是展开
if
(isOpen == YES) {
//xxxxxx
}else{
//收起
}
//不是自身
}
else {
}
当点击时候在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//将索引加到数组中
NSArray
*indexPaths = [NSArray arrayWithObject:indexPath];
//判断选中不同row状态时候
// if
(self.selectedIndex != nil && indexPath.row != selectedIndex.row) {
if
(self.selectedIndex != nil && indexPath.row == selectedIndex.row) {
//将选中的和所有索引都加进数组中
// indexPaths
= [NSArray arrayWithObjects:indexPath,selectedIndex, nil];
isOpen
= !isOpen;
}else
if (self.selectedIndex != nil && indexPath.row != selectedIndex.row) {
indexPaths
= [NSArray arrayWithObjects:indexPath,selectedIndex, nil];
isOpen
= YES;
}
//记下选中的索引
self.selectedIndex
= indexPath;
//刷新
[tableView
reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
经过不断调试,终于实现了点击任意一个cell展开收缩效果
首先要理解UITableView代理方法调用的先后顺序。
当初始化UITableView后,代理回调顺序如下
1://返回cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2://返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
3://请求数据元代理为tableView插入需要的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4://监听点击的cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
需要声明一个全局BOOL变量isOpen,记录当前cell的状态,声明一个NSIndexPath类型selectedIndex,记录选择的cell的row。
在heightForRowAtIndexPath代理里面实现//选中状态返回的高度
if
(indexPath.row == selectedIndex.row && selectedIndex != nil ) {
if
(isOpen == YES) {
//cell上的label高度自适应
CGSize
size = [textStr sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(290, 1000) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat
f = size.height;
if
(indexPath.row == [self.dataArr count]-1){
return
153.8+(f - 21);
}
return
155+(f - 21);
}else{
return
67;
}
}
同样在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里实现一样的条件
if
(indexPath.row == selectedIndex.row && selectedIndex != nil) {
//如果是展开
if
(isOpen == YES) {
//xxxxxx
}else{
//收起
}
//不是自身
}
else {
}
当点击时候在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//将索引加到数组中
NSArray
*indexPaths = [NSArray arrayWithObject:indexPath];
//判断选中不同row状态时候
// if
(self.selectedIndex != nil && indexPath.row != selectedIndex.row) {
if
(self.selectedIndex != nil && indexPath.row == selectedIndex.row) {
//将选中的和所有索引都加进数组中
// indexPaths
= [NSArray arrayWithObjects:indexPath,selectedIndex, nil];
isOpen
= !isOpen;
}else
if (self.selectedIndex != nil && indexPath.row != selectedIndex.row) {
indexPaths
= [NSArray arrayWithObjects:indexPath,selectedIndex, nil];
isOpen
= YES;
}
//记下选中的索引
self.selectedIndex
= indexPath;
//刷新
[tableView
reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
经过不断调试,终于实现了点击任意一个cell展开收缩效果
相关文章推荐
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- IOS应用开发11——UITableView的cell点击展开
- UITableView点击展开cell
- 用UICollectionView实现点击展开收缩的cell
- UITableView中Cell点击展开方法
- iOS点击UITableView的cell,弹出UIAlertController有时会出现延迟
- UITableView的点击删除或者插入一条Cell
- [iOS]点击UITableView的Cell紧随其后出现一行功能栏
- ios UITableView 获取点击cell对象
- 设置UITableView背景透明/监听cell左边的删除按钮的点击事件
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath
- Swift - 实现点击UITableView单元格时自动展开单元格