设计了个小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;