ios - 如何在点击时更改 UITableViewCell 的高度?

标签 ios swift uitableview cocoa-touch delegates

我试图让 UITableViewCell 在点击特定单元格时显示有关特定单元格的更多内容。一旦单元格展开并再次被点击,单元格应收缩回其原始大小。

我很确定必须对委托(delegate) heightForRowAtIndexPathdidSelectRowAtIndexPath 做一些事情,但我不知道如何使用 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/

相关文章:

jquery - anchor 链接在 Iphones/iOS 设备中不可点击

swift - 枚举数组是 Swift 中的枚举?

带有 JSON 数组的 iOS swift tableView header

ios - 检测点击 UITableView 的空白区域

ios - Facebook 个人资料图片无法下载

ios - 用户登录一次后,我在设置初始化 View Controller 时遇到问题

ios - 使用多个 UICollectionView,出现错误, "... must be initialized with non-nil layout"

swift - 如何使用 Swift 4 从 firebase 中的 child 那里获取特定值?

ios - Monotouch - UIViewController 内的 TableView : override functions don't get called

ios - 如何将 "import"UIView XIB 转换为 UIViewController XIB?