我试图让 UITableViewCell 在点击特定单元格时显示有关特定单元格的更多内容。一旦单元格展开并再次被点击,单元格应收缩回其原始大小。
我很确定必须对委托(delegate) heightForRowAtIndexPath
和 didSelectRowAtIndexPath
做一些事情,但我不知道如何使用 didSelectRowAtIndexPath 选择特定的表格单元格行.
// Height of table cell rows
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 45
}
//On cell tap, expand
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.rowHeight = 75;
}
此外,是否可以隐藏从父单元格溢出的任何内容?有点像 overflow: hidden;在 CSS 中。
最佳答案
在 didSelectRowAtIndexPath 中选择的 TableView 上声明一个全局变量 NSInteger type 并存储 row 并在此处重新加载。检查 heightForRowAtIndexPath 中的行并在那里增加高度。 像这样尝试
var selectedIndex : NSInteger! = -1 //Delecre this global
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == selectedIndex{
selectedIndex = -1
}else{
selectedIndex = indexPath.row
}
tableView.reloadData()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == selectedIndex
{
return 75
}else{
return 45
}
}
关于ios - 如何在点击时更改 UITableViewCell 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32718195/