我按照这个应用程序构建了一个包含自定义单元格的表格:https://github.com/awseeley/Custom-Table-Cell
我正在尝试展开一个单元格并在单击该单元格时显示不同的内容。
这是我的 View Controller 中的内容:
var cellTapped:Bool = true
var currentRow = 0;
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
let selectedRowIndex = indexPath
currentRow = selectedRowIndex.row
let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TblCell
cell.image.hidden = true
tableView.beginUpdates()
tableView.endUpdates()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == currentRow {
if cellTapped == false {
cellTapped = true
return 141
} else {
cellTapped = false
return 70
}
}
return 70
}
单元格在单击时展开,再次单击时缩小。但图像并没有隐藏。如何让图像隐藏?有没有更好的方法来在单元格被选中并具有展开/折叠效果时显示另一个自定义 .xib 文件?
最佳答案
您应该将此工具添加到您的
cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == currentRow { if cellTapped == false { cell.image.hidden = false } else { cell.image.hidden = true } } return cell }
并且 didSelectRowAtIndexPath
您应该删除错误的代码:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
let selectedRowIndex = indexPath
currentRow = selectedRowIndex.row
tableView.reloadData()
}
如果你想使用tableView.beginUpdate()来获得更好的动画效果,可以引用我的演示:
希望对您有所帮助!
关于ios - 在表格单元格中隐藏选择元素 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34602532/