【问题标题】:The indexPath of a specific UITableViewCell is always nil特定 UITableViewCell 的 indexPath 始终为 nil
【发布时间】:2016-10-02 10:06:12
【问题描述】:

更新到 Xcode8 后,以下代码不再工作

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


    let cell = collectionView.superview!.superview as! UITableViewCell
    let table = cell.superview!.superview as! UITableView
    let indexPath = table.indexPath(for: cell)

现在 indexPath 的值总是 nil。 有什么想法吗?

【问题讨论】:

  • 检查调试器中的层次结构,从collectionView开始,向上运行superview列表。想清楚后,把答案贴在这里。
  • 我已经检查了层次结构和超级视图列表。一切似乎都还好。我可以在调试器中看到 indexPath 有一个值,但是在“let indexPath = table.indexPath(for:cell)”行之后,该值变为 nil。

标签: uitableview uicollectionview xcode8 indexpath


【解决方案1】:

我已经解决了问题,将最后一行替换为

let indexPath = table.indexPathForRow(at: cell.center)

【讨论】:

  • 谁能解释一下,为什么这种方式有效而另一种方式无效?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多