【问题标题】:TableView Cell doesn't scale equally along y-axisTableView Cell 沿 y 轴的缩放比例不同
【发布时间】:2020-05-16 06:11:32
【问题描述】:

我有在每个单元格中填充有颜色的 tableView 单元格。我想要的是当用户点击单元格时,它会“打开”/展开,以便该颜色填充整个屏幕。目前,它仅从我单击的单元格向下缩放。我还需要它沿 y 轴向上缩放,每个单元格都扩展到屏幕顶部,但我不确定是什么禁止它。

let expandedColorView: UIView = {
        let view = UIView()
        return view
    }()

@objc func userTap(sender: UITapGestureRecognizer) {

        if sender.state == UIGestureRecognizer.State.ended {
            let tapLocation = sender.location(in: self.paletteTableView)

            if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
                if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) {
                    UIView.animate(withDuration: 1.0, animations: {
                        tappedCell.transform = CGAffineTransform(scaleX: 1, y: 50)
                    } )

                }
            }
        }
    }

UITapGestureRecognizer 在 tableView(cellForRowAt:) 中声明,cell.isUserInteractionEnabled = true 已启用。

我尝试在UIView.animate 中更改扩展颜色视图边界self.expandedColorView.bounds.size.height = UIScreen.main.bounds.height,但这并没有改变任何东西。我在想单元格的框架需要更改,以便它与父视图框架(我认为应该是 tableView)匹配,但我不知道该怎么做。

任何帮助将不胜感激!

我附上了问题的 gif:

【问题讨论】:

    标签: ios swift uitableview cgaffinetransform


    【解决方案1】:

    如果这是你想要的

    这是我在数据源扩展中所做的

    extension ViewController: UITableViewDataSource {
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if let cell = self.tableView.dequeueReusableCell(withIdentifier: "colorCell") as? ColorFulTableViewCell {
                let color = colors[Int(indexPath.row % 7)]
                cell.backgroundColor = color
                return cell
            }
            return UITableViewCell()
        }
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 50
        }
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if let row = expandCell?.row, row == indexPath.row {
                return self.tableView.bounds.size.height
            }
            else {
                return 100
            }
        }
    }
    

    tableView 委托扩展看起来像

    extension ViewController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if self.expandCell == indexPath { return }
            else {
                self.expandCell = indexPath
            }
            self.tableView.reloadRows(at: [indexPath], with: .automatic)
            self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
        }
    }
    

    她怎么了?

    在 TableView 数据源方法的 heightForRowAt 中,我检查单元格是否需要使用 if let row = expandCell?.row, row == indexPath.row { 覆盖整个 tableView 大小,并通过返回 self.tableView.bounds.size.height 设置其高度以匹配 tableView 高度,否则我返回 100

    didSelectRowAt 中,我通过将其保存在expandCell 中来更新单元格的indexPath 以展开并重新加载行(这样,当调用行的高度时,它可以返回self.tableView.bounds.size.height,我也调用@987654334 @ 位置为.top 以确保我的单元格滚动到顶部并使其完全可见

    因为你只重新加载一个特定的单元格,虽然从成本的角度来看它是有效的,但是动画可能看起来很生锈,因为可见 indexPath 数组中的其他单元格正在突然调整它们,你总是可以调用 reload Data 以获得更好的更流畅的体验。

    extension ViewController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if self.expandCell == indexPath { return }
            else {
                self.expandCell = indexPath
            }
            self.tableView.reloadData()
            self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 非常感谢您的解释和帮助。问题 - expandCell 是我在某处定义的对象吗?我收到一条错误消息,指出“使用未解析的标识符 'expandCell'”。如果启用 tableView 滚动,我只能扩展单元格吗?在你发布之后我意识到我应该在我的代码中更彻底,因为我有tableView.isScrollEnabled = false(在 SO 上发布仍然是新手)。当我将其设置为 true 时,单元格能够缩放到顶部和底部,但仍然不稳定,因为它周围的单元格会在展开时显示在其顶部
    • 我对我的代码进行了一些更改,它的不同之处足以让我将它作为一个新问题发布,如果你可以查看stackoverflow.com/questions/61847668/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2017-01-15
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    相关资源
    最近更新 更多