我有一个包含 UICollectionView
的 UITableViewCell
。在 UICollectionView
中,每个“行”总共可以容纳 4 个 UIImages
。我很难弄清楚如何确定何时添加第 n 个元素以生成“下一个”行的逻辑,以便我可以重新加载 TableView 行以扩展其 UITableViewCell
高度。
例如,下面是 UITableViewCell
中第一行有 4 张图片的样子:
[ x x x x]
在我的代码中,当添加第 5 个图像时,UITableViewCell
的高度会扩展以适应附加的第 5 个图像:
[ x x x x]
[ 5 ]
它继续第 5、9、13、17 等图像,我想在重新加载表格 View 行以扩展 UITableViewCell
的高度以适应下一组之前检查图像。
图像被添加到一个数组中,目前我正在做这样的事情:
if (images.count % 5) == 0
{
self.imagesTableView.reloadRows(at: [ IndexPath(row: 0, section: 4) ],
with: UITableViewRowAnimation.none)
}
很明显,逻辑只有在它是 5 的倍数时才有效。我觉得这是一个简单的逻辑检查,但我终究无法弄清楚。
有人能指出我正确的方向吗?
谢谢!
最佳答案
您的情况的正确条件是
let isMultiple = ((i-1) % n) == 0
或者
let isMultiple = (i % n) == 1
实现的想法是这样的:
let itemsPerRow = 4
let filter = { (itemNumber: Int) -> Bool in
return (itemNumber > itemsPerRow) && ((itemNumber % itemsPerRow) == 1)
}
print(Array(1...20).filter(filter))
// prints [5, 9, 13, 17]
关于ios - 如何确定何时添加了第 n 个元素以重新加载 Swift 中的 TableView 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227249/