【问题标题】:Place UITableViewCell beneath siblings将 UITableViewCell 放在兄弟姐妹的下方
【发布时间】:2015-06-24 12:09:04
【问题描述】:

我正在尝试构建一个自定义 UITableView,其行为方式与提醒应用类似。

我希望它被下一个单元格覆盖,而不是最上面的可见单元格从显示屏上滚动出来,这样当您滚动时,这些单元格看起来会相互堆叠。

目前我正在使用:

override func scrollViewDidScroll(scrollView: UIScrollView) {

  let topIndexPath: NSIndexPath = tableView.indexPathsForVisibleRows()?.first as! NSIndexPath

  let topCell = tableView.cellForRowAtIndexPath(topIndexPath)

  let frame = topCell!.frame

  topCell!.frame = CGRectMake(frame.origin.x, scrollView.contentOffset.y, frame.size.width, frame.size.height)
}

但顶部单元格始终位于第二个单元格上方 - 导致第二个单元格在其下方滚动。

此外,如果我快速滚动,这似乎会放错我所有的单元格。

编辑:已修复此问题。答案贴在下面以供将来参考。

【问题讨论】:

    标签: ios swift uitableview cocoa-touch


    【解决方案1】:

    对于将来搜索此内容的任何人。 只需遍历所有可见单元格并将其 z 位置设置为其行号(因此每个单元格都堆叠在前一个单元格之上)。

    if 语句告诉顶部单元格保持在滚动视图的 contentOffset 处,并让所有其他单元格占据它们的预期位置。如果滚动太快,这会阻止其他单元格偏移。

    override func scrollViewDidScroll(scrollView: UIScrollView) {
    
      // Grab visible index paths
      let indexPaths: Array = tableView.indexPathsForVisibleRows()!
    
      var i = 0
      for path in indexPaths {
        let cell = tableView.cellForRowAtIndexPath(path as! NSIndexPath)
        // set zPosition to row value (for stacking)
        cell?.layer.zPosition = CGFloat(path.row)
    
        // Check if top cell (first in indexPaths)
        if (i == 0) {
          let frame = cell!.frame
    
          // keep top cell at the contentOffset of the scrollview (top of screen)
          cell!.frame = CGRectMake(frame.origin.x,
                                    scrollView.contentOffset.y,
                                    frame.size.width,
                                    frame.size.height)
        } else {
          // set cell's frame to expected value
          cell!.frame = tableView.rectForRowAtIndexPath(path as! NSIndexPath)
        }
        i++
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多