您的位置:首页 > 其它

关于cell点击展开收缩的总结

2017-06-26 09:14 211 查看
在最近的项目开发,为了更好的用户体验,项目决定使用一些cell默认展开一条,并具有点击展开和收缩的功能。经过查看资料,后来通过cell 属性实现了该功能,在开发中也遇到了一些坑,在此总结,以供参考。


@property (nonatomic, strong) NSIndexPath *selectedIndex;


首先声明一个选中的index对象,注意:这里的语义属行,在刚开始我用的assign在一些机型上实现。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
self.selectedIndex = indexPath;


设置默认选中状态,即选中的cell的index.

if ([indexPath isEqual: _selectedIndex] && _selectedIndex != nil  ) {
if (_isOpen == YES) {
projectCell.isOpen = self.isOpen;
}else{
projectCell.isOpen = NO;
}
} else {
projectCell.isOpen = NO;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
设置选中cell要做的事情

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

NSArray *indexPaths = [NSArray arrayWithObject:indexPath];

if (self.selectedIndex != nil && [indexPath isEqual: _selectedIndex]) {
_isOpen = !_isOpen;

}else if (self.selectedIndex != nil && ![indexPath isEqual: _selectedIndex]) {
indexPaths = [NSArray arrayWithObjects:indexPath,_selectedIndex, nil];
_isOpen = YES;
}    //记下选中的索引
self.selectedIndex = indexPath;

//刷新
[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}


设置cell选中时的操作,当然还用要计算行高,这些我会稍后将写个demo上传到GitHub和大家交流,在这个开发中我充分认识到我们对底层研究的重要性!!!

欢迎大家一起交流!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: