简体   繁体   English

While循环在Swift中无法正确检查

[英]While loop not checking correctly in Swift

Currently I have a while statement that loops through all cells below that are of a lower padding level and is supposed to remove said cells. 目前,我有一个while语句,它遍历填充级别较低的所有下面的单元格,并应该删除这些单元格。 Currently the below statement goes through about 3 of them then stops even though further cells meet the conditions. 当前,即使其他单元格满足条件,下面的语句也将遍历其中的3条,然后停止。

if isExpanded[indexPath.item] //if already expanded remove
{
    var t = 1

    while (comment.paddingLevel != commentsData[indexPath.row + t].paddingLevel)
    {
        isExpanded.removeAtIndex(indexPath.row + t)

        preRenderedText.removeAtIndex(indexPath.row + t)
        commentsData.removeAtIndex(indexPath.row + t)

        var path = NSIndexPath(forItem: indexPath.row + t, inSection: 0)
        t++
        delpaths.append(path)        
    }
}

You are mutating the isExpanded array by deleting items from it - this means that the index for the next item is going to reduce by one, but you increment t regardless. 您正在通过从isExpanded数组中删除项目来对其进行突变-这意味着下一项的索引将减少一,但是无论如何您都将t递增。 An NSMutableIndexSet is probably a better data structure to use for isExpanded rather than an array. NSMutableIndexSet可能是用于isExpanded而不是数组的更好的数据结构。

An NSMutableIndexSet stores a set of integer indices - In this case it would be your item values. NSMutableIndexSet存储一组整数索引-在这种情况下,它将是您的item值。 Then you can use the set's contains(item) method to quickly get an answer as to whether a particular index is in the set or not. 然后,您可以使用集合的contains(item)方法快速获取有关特定索引是否在集合中的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM