当我搜索到详细信息屏幕时,为什么表格视图单元格会移动?

时间:2019-04-12 15:51:51

标签: swift uitableview segue

我有一个UIViewController,其中包含一个UITableView(与使用UITableViewController相对),它做的事情很奇怪。

当我点击一个单元格以切换到详细信息屏幕时,当我点击“后退”按钮时,被点击的单元格(通常但并非总是)比点击该屏幕时位于屏幕的上方。有时甚至完全看不见。

在诊断问题时,我注意到最奇怪的事情是放慢了敲击单元格时发生的动画的速度。从表格视图屏幕切换到详细信息屏幕时,被点击的单元格会自我复制,然后向上浮动。

我在过渡期间抓取了一张屏幕截图。您可以在此处看到我点击了Cell 4的位置后,它立即复制了自己,并在看不见时开始向顶部上升。

enter image description here

经过大量研究,我认为问题很可能与以下事实有关:单元格正在自动调整大小(因为每个单元格中的文本量可以变化),并且表格视图正尝试计算单元格的高度。我已经确定:

  • 单元格中的所有子视图都具有顶部,底部和侧面约束(我什至删除了几个堆栈视图来做到这一点)
  • 我设置了tableView.estimatedRowHeight = 160
  • 我设置了tableView.rowHeight = UITableView.automaticDimension

我没有在tableview委托方法中做任何异常的事情:

extension DocumentViewController: UITableViewDataSource, UITableViewDelegate {
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "documentTableViewCell", for: indexPath) as! DocumentTableViewCell
        cell.tag = indexPath.row
        cell.updateCellProperties(publication: viewModel.data[indexPath.row], indexPathRow: indexPath.row)
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: showDocumentSegueIdentifier, sender: self)
    }
} 

这就是cell.updateCellProperties中发生的事情:

func updateCellProperties(document: DocumentModel, indexPathRow: Int) {
    selectedBackgroundView?.backgroundColor = UIColor.clear

    authorName.text = document.formattedAuthor.name
    authorInitials.text = document.formattedAuthor.initials
    documentTitle.text = document.title
    setAuthorImage(document, indexPathRow: indexPathRow)
    setDocTypeBadge(document: document, indexPathRow: indexPathRow)
    unreadCommentCount.text = ""

    // set read/unread style
        var alphaValue = CGFloat(1.0)
        if let isRead = document.isRead {
          if isRead {
            contentView.backgroundColor = UIColor.white
            alphaValue = CGFloat(0.5)
          } else {
            contentView.backgroundColor = UIColor.lightGrey3
          }
          authorName.alpha = alphaValue
          authorInitials.alpha = alphaValue
          authorImage.alpha = alphaValue
          documentTitle.alpha = alphaValue
          unreadCommentCount.alpha = alphaValue
          docTypeBadge.alpha = alphaValue
        }
  }

prepare(for segue:)中没有发生任何有趣的事情:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let path = tableView.indexPathForSelectedRow
    if (segue.identifier == showDocumentSegueIdentifier) {
        let controller = segue.destination as! DocumentViewController
        controller.hidesBottomBarWhenPushed = true
    }
}

我觉得我已经正确地完成了所有操作,但是这种奇怪的行为不会消失。您对我可以尝试的其他方法有什么建议吗?

谢谢

0 个答案:

没有答案