gpt4 book ai didi

ios - 将 UITableViewCell 放在 sibling 下面

转载 作者:可可西里 更新时间:2023-10-31 23:44:45 24 4
gpt4 key购买 nike

我正在尝试构建一个与提醒应用类似的自定义 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)
}

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

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

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

最佳答案

供将来搜索此内容的任何人使用。只需循环遍历所有可见单元格并将它们的 z 位置设置为它们的行号(这样每个单元格都堆叠在前一个单元格之上)。

if 语句告诉顶部单元格保持在 ScrollView 的 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++
}

}

关于ios - 将 UITableViewCell 放在 sibling 下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com