【问题标题】:Expandable table cell content overflowing from cell可扩展的表格单元格内容从单元格溢出
【发布时间】:2019-07-16 03:08:18
【问题描述】:

我的项目中有一个可扩展的 tableview 作为我的 tableviewcontroller 的一部分。问题是当表格没有展开时,单元格的其余部分无论如何都会显示,与下一个单元格重叠。我用来创建可扩展表的方法来自本教程:https://dev.to/lawgimenez/implementing-the-expandable-cell-in-ios-uitableview-f7j

我认为他们改变了一些东西,因为这段代码是用旧版本的 xcode 和 swift 编写的。

我已尝试更改单元格和表格视图的某些属性,但到目前为止没有任何效果。

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

             tableViewData[indexPath.row].opened = !tableViewData[indexPath.row].opened

             tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

            if tableViewData[indexPath.row].opened == true {
                return 99.5
            }
            else {
                return 40.5
            }

    }

我希望 tableview 中的单元格在 tableview 中单元格的确切高度处切断自定义单元格,但是当不展开时单元格很小,但所有自定义单元格都被显示并从分配的区域溢出细胞。

【问题讨论】:

    标签: swift xcode uitableview


    【解决方案1】:

    你应该使用

    tableView.reloadRows(at: [indexPath], with: .automatic)
    

    而不是

    tableView.reloadData()
    

    另一件事,你应该使用

    if tableViewData[indexPath.row].opened {
    
    }
    

    而不是

    if tableViewData[indexPath.row].opened == true {
    
    }
    

    【讨论】:

    • 而且他可以用tableViewData[indexPath.row].opened.toggle()代替tableViewData[indexPath.row].opened = !tableViewData[indexPath.row].opened
    • 嗨 @MiguelAngelOlmedoPerez 欢迎来到堆栈溢出。如果您能多解释一下为什么要提出代码建议,那就太好了 - 例如, 传递附加的 at: 和 with: 参数来指定 baz 中 bar 的 foo,如果你把它们排除在外的话得到 gazoink。
    猜你喜欢
    • 2016-09-28
    • 2015-12-11
    • 2013-03-15
    • 1970-01-01
    • 2017-12-05
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多