Ios 如何确定何时添加第n个元素以在Swift中重新加载表视图行?

Ios 如何确定何时添加第n个元素以在Swift中重新加载表视图行?,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我有一个包含UICollectionView的UITableViewCell。在UICollectionView中,每一行可以容纳总共4个UIImages。我很难弄清楚如何确定何时添加第n个元素以生成“下一行”的逻辑,这样我就可以重新加载表视图行以扩展其UITableViewCell高度 例如,UITableViewCell中的第一行显示4幅图像: [ x x x x] 在我的代码中,当添加第五幅图像时,UITableViewCell的高度会扩展以适合附加的第五幅图像: [ x x x x

我有一个包含
UICollectionView
UITableViewCell
。在
UICollectionView
中,每一行可以容纳总共4个
UIImages
。我很难弄清楚如何确定何时添加第n个元素以生成“下一行”的逻辑,这样我就可以重新加载表视图行以扩展其
UITableViewCell
高度

例如,
UITableViewCell
中的第一行显示4幅图像:

[ x x x x]  
在我的代码中,当添加第五幅图像时,
UITableViewCell
的高度会扩展以适合附加的第五幅图像:

[ x x x x]
[ 5      ]
接着是第5、9、13、17等图像,我想在重新加载表视图行之前检查这些图像,以扩展
UITableViewCell
的高度,以适应下一组图像

图像被添加到一个数组中,目前我正在做类似的事情:

if (images.count % 5) == 0
{
    self.imagesTableView.reloadRows(at: [ IndexPath(row: 0, section: 4) ],
                                            with: UITableViewRowAnimation.none)
}
显然,只有当它是5的倍数时,逻辑才起作用。我觉得这是一个简单的逻辑检查,但我一辈子都搞不懂

有人能给我指出正确的方向吗


谢谢

我可能完全没有抓住你问题的重点,但是

我认为应该是:

if (images.count % 4) == 1
{
    self.imagesTableView.reloadRows(at: [ IndexPath(row: 0, section: 4) ],
                                            with: UITableViewRowAnimation.none)
}

如果您愿意,您也可以检查(images.count>5),以防在第一个添加的图像上运行的代码出现问题。

我可能完全没有理解您的问题,但是

我认为应该是:

if (images.count % 4) == 1
{
    self.imagesTableView.reloadRows(at: [ IndexPath(row: 0, section: 4) ],
                                            with: UITableViewRowAnimation.none)
}

如果需要,您还可以检查(images.count>5),以防在第一个添加的图像上运行的代码出现问题。

看起来您需要的是以下内容:

let previousCount = self.images.count
self.images.append(newElement) // This is where you are updating your images array

let previousNumberOfRows = (previousCount / 4) + 1 // May want to explicitly check the case where previousCount == 0 if you don't want any rows to show for that case
let updatedNumberOfRows = (self.images.count / 4) + 1 // Again, may want to handle 0 items case
let rowDiff = updatedNumberOfRows - previousNumberOfRows

guard rowDiff != 0 else {
  return
}

let range: CountableRange
if rowDiff > 0 {
  range = (0 ..< rowDiff)
} else {
  range = (rowDiff ..< 0)
}

let indexPathsToReload = range.flatMap { sectionIndex in 
    return IndexPath(row: sectionIndex + previousNumberOfSections, section: 0)
}
self.imagesTableView.reloadRows(at: indexPathsToReload)
让previousCount=self.images.count
self.images.append(newElement)//这是更新图像数组的地方
让previousNumberOfRows=(previousCount/4)+1//如果不希望为该情况显示任何行,则可能需要显式检查previousCount==0的情况
同样,让updatedNumberOfRows=(self.images.count/4)+1//可能需要处理0个项目
让rowDiff=updatedNumberOfRows-previousNumberOfRows
守卫rowDiff!=0其他{
返回
}
let范围:可数范围
如果rowDiff>0{
范围=(0..

我必须快速键入此答案,因此可能会出现一个一个接一个的错误。让我知道进展如何

看起来你想要的东西大致如下:

let previousCount = self.images.count
self.images.append(newElement) // This is where you are updating your images array

let previousNumberOfRows = (previousCount / 4) + 1 // May want to explicitly check the case where previousCount == 0 if you don't want any rows to show for that case
let updatedNumberOfRows = (self.images.count / 4) + 1 // Again, may want to handle 0 items case
let rowDiff = updatedNumberOfRows - previousNumberOfRows

guard rowDiff != 0 else {
  return
}

let range: CountableRange
if rowDiff > 0 {
  range = (0 ..< rowDiff)
} else {
  range = (rowDiff ..< 0)
}

let indexPathsToReload = range.flatMap { sectionIndex in 
    return IndexPath(row: sectionIndex + previousNumberOfSections, section: 0)
}
self.imagesTableView.reloadRows(at: indexPathsToReload)
让previousCount=self.images.count
self.images.append(newElement)//这是更新图像数组的地方
让previousNumberOfRows=(previousCount/4)+1//如果不希望为该情况显示任何行,则可能需要显式检查previousCount==0的情况
同样,让updatedNumberOfRows=(self.images.count/4)+1//可能需要处理0个项目
让rowDiff=updatedNumberOfRows-previousNumberOfRows
守卫rowDiff!=0其他{
返回
}
let范围:可数范围
如果rowDiff>0{
范围=(0..

我必须快速键入此答案,因此可能会出现一个一个接一个的错误。让我知道进展如何

您的情况的正确条件是

let isMultiple = ((i-1) % n) == 0

实施的想法如下:

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]

你的情况正确的条件是

let isMultiple = ((i-1) % n) == 0

实施的想法如下:

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]

我希望能够输入if check which images.count=5,(5)+4,(5+4)+4,(5+4+4)+4等,因此您的代码答案无法解决我的问题我希望能够输入if check which images.count=5,(5)+4,(5+4)+4,(5+4+4)+4等,因此您的代码答案无法解决我的问题
let isMultiple=((i-1)%n)==0绝对是我所需要的逻辑…如此简单,我无法相信我无法理解它。
让isMultiple=((i-1)%n)==0绝对是我所需要的逻辑…如此简单,我无法相信我无法理解它。。