UITableView上的cell自动收缩

设计了个小demo,显示时,cell为一行,当单击某个cell时,cell会展开,再次单击,则cell收回。该方法可以多行同时展开。

这个demo的做法是

      1.首先定义 var selectedCellIndexPath:[NSIndexPath] = [];

      2.在cellForRowAtIndexPath的方法中,构建cell,假设cell为两部分,上部分为UILabel,下部分为UITextField,利用cell.contentView.addSubview(label);

cell.contentView.addSubview(textview);添加两部分,在cellForRowAtIndexPath利用SnapKit框架进行snp_makeConstraints进行约束,将label高度设置不展开的高度,如40,textview高度设置为80,(则展开后高度为120)。

        3.在didSelectRowAtIndexPath方法中设置选中效果,代码为

self.tableView.deselectRowAtIndexPath(indexPath, animated: true);

if let index = selectedCellIndexPath.indexOf(indexPath) {

selectedCellIndexPath.removeAtIndex(index);

} else {

selectedCellIndexPath.append(indexPath);

}

self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None);

      4.在heightForRowAtIndexPath方法中确定高度,代码为

if self.selectedCellIndexPath.count != 0 && self.selectedCellIndexPath.contains(indexPath) {

return 120;

}

return 40;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容