简体   繁体   English

UICollectionView.reloadData()更改单元格顺序| iOS Swift

[英]UICollectionView.reloadData() changes cell order | iOS Swift

So I am working with a simple UICollectionView, 3 columns by X rows, where X is set by the variable matchesFromSelectedSession.count + 1. Here are the delegate functions that I am using for my collectionView: 因此,我正在使用一个简单的UICollectionView,按X行3列,其中X由变量matchsFromSelectedSession.count + 1设置。这是我用于collectionView的委托函数:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let cellHeight = 15.0 as CGFloat
    return CGSizeMake(collectionView.bounds.size.width/3, cellHeight)
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return matchesFromSelectedSession.count + 1
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let reuseIdentifier = "MatchCollectionViewCell"
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MatchCollectionViewCell
    cell.matchCollectionLabel = UILabel(frame: CGRectMake(0,0,collectionView.bounds.width/3 - 3.0,15))
    cell.matchCollectionLabel.textColor = UIColor.blackColor()
    cell.systemLayoutSizeFittingSize(cell.frame.size, withHorizontalFittingPriority: UILayoutPriorityDefaultHigh, verticalFittingPriority: UILayoutPriorityDefaultLow)
    cell.matchCollectionLabel.font = UIFont(name: "Helvetica Neue", size:12)
    if indexPath.section == 0 {
        switch indexPath.row {
        case 0:
            cell.matchCollectionLabel.text = "Match #"
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            cell.matchCollectionLabel.textAlignment = NSTextAlignment.Right
            break
        case 1:
            cell.matchCollectionLabel.text = "Record"
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            cell.matchCollectionLabel.textAlignment = NSTextAlignment.Center
            break
        case 2:
            cell.matchCollectionLabel.text = "Outcome"
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            break
        default:
            cell.matchCollectionLabel.text = ""
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            break
        }
    } else {
        switch indexPath.row {
        case 0:
            cell.matchCollectionLabel.text = "\(indexPath.section)"
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            cell.matchCollectionLabel.textAlignment = NSTextAlignment.Right
            break
        case 1:
            cell.matchCollectionLabel.text = matchesFromSelectedSession[indexPath.section - 1].matchRecord()
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            cell.matchCollectionLabel.textAlignment = NSTextAlignment.Center
            break
        case 2:
            let outcome = matchesFromSelectedSession[indexPath.section - 1].matchOutcome()
            switch outcome {
            case "W":
                cell.matchCollectionLabel.text = "Win"
                cell.matchCollectionLabel.textColor = UIColor.greenColor()
                break
            case "D":
                cell.matchCollectionLabel.text = "Draw"
                cell.matchCollectionLabel.textColor = UIColor.blueColor()
                break
            case "L":
                cell.matchCollectionLabel.text = "Loss"
                cell.matchCollectionLabel.textColor = UIColor.redColor()
                break
            default:
                cell.matchCollectionLabel.textColor = UIColor.blackColor()
                break
            }
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            break
        default:
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            cell.matchCollectionLabel.text = ""
            break
        }
    }
    //add subview if new cell otherwise tag cell to know to reuse next time
    if cell.tag != 19 {
        cell.addSubview(cell.matchCollectionLabel)
    }
    cell.tag = 19
    return cell
}

The custom cell I use, MatchCollectionViewCell, only contains one label, matchCollectionLabel and that is it. 我使用的自定义单元MatchCollectionViewCell仅包含一个标签matchCollectionLabel,就是这样。 I use self.matchCollectionView.reloadData() in the viewDidAppear function of the view for when a secondary view changes the table and dismisses back to this original view. 我在视图的viewDidAppear函数中使用self.matchCollectionView.reloadData()来查看辅助视图何时更改表并将其退回到原始视图。

I tag the cells after the first use that I am not constantly adding unnecessary subviews to the cell after every viewDidAppear. 第一次使用后,我会标记单元格,因为我不会在每个viewDidAppear之后不断向单元格中添加不必要的子视图。

The table looks great after the initial setup but after reloadData is called, the collectionView's cell order gets changed and jumbled. 初始设置后,该表看起来不错,但是在调用reloadData之后,collectionView的单元格顺序被更改并混乱。 If I edit the number of cells, reloadData() updates to correctly show the cell number change but the cells are still in an incorrect order. 如果我编辑单元格数,则reloadData()更新以正确显示单元格数更改,但是单元格的顺序仍然不正确。

Here is a picture of before and after reloadData 这是reloadData之前和之后的图片

http://imgur.com/wqfXqLG.png http://imgur.com/wqfXqLG.png

Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

You are creating a new label every time. 您每次都在创建一个新标签。 You only add it as a subview if the tag isn't 19 but you always allocate a new label. 如果标记不是19,则仅将其添加为子视图,但始终分配新标签。

This means that when you reuse a cell, you are updating a new label, not the one that has been added to the cell, so your changes aren't visible. 这意味着,当您重复使用单元格时,您将更新一个新标签,而不是已添加到该单元格的标签,因此您的更改不可见。

You can either move your tag check to the top of the function and use that to control the allocation of the label, or define matchCollectionLabel as an optional and then you can check it for nil : 您可以将标记检查移至函数顶部,并使用它来控制标签的分配,或者将matchCollectionLabel定义为可选,然后可以将其检查为nil

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let reuseIdentifier = "MatchCollectionViewCell"
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MatchCollectionViewCell

    if cell.matchCollectionLabel == nil {
        cell.matchCollectionLabel = UILabel(frame: CGRectMake(0,0,collectionView.bounds.width/3 - 3.0,15))
        cell.matchCollectionLabel!.textColor = UIColor.blackColor()
        cell.systemLayoutSizeFittingSize(cell.frame.size, withHorizontalFittingPriority: UILayoutPriorityDefaultHigh, verticalFittingPriority: UILayoutPriorityDefaultLow)
        cell.matchCollectionLabel!.font = UIFont(name: "Helvetica Neue", size:12)
        cell.addSubview(cell.matchCollectionLabel!)
    }

    if indexPath.section == 0 {
        switch indexPath.row {
        case 0:
            cell.matchCollectionLabel!.text = "Match #"
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            cell.matchCollectionLabel!.textAlignment = NSTextAlignment.Right
            break
        case 1:
            cell.matchCollectionLabel!.text = "Record"
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            cell.matchCollectionLabel!.textAlignment = NSTextAlignment.Center
            break
        case 2:
            cell.matchCollectionLabel!.text = "Outcome"
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            break
        default:
            cell.matchCollectionLabel!.text = ""
            cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            break
        }
    } else {
        switch indexPath.row {
        case 0:
            cell.matchCollectionLabel!.text = "\(indexPath.section)"
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            cell.matchCollectionLabel!.textAlignment = NSTextAlignment.Right
            break
        case 1:
            cell.matchCollectionLabel!.text = matchesFromSelectedSession[indexPath.section - 1].matchRecord()
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            cell.matchCollectionLabel!.textAlignment = NSTextAlignment.Center
            break
        case 2:
            let outcome = matchesFromSelectedSession[indexPath.section - 1].matchOutcome()
            switch outcome {
            case "W":
                cell.matchCollectionLabel!.text = "Win"
                cell.matchCollectionLabel!.textColor = UIColor.greenColor()
                break
            case "D":
                cell.matchCollectionLabel!.text = "Draw"
                cell.matchCollectionLabel!.textColor = UIColor.blueColor()
                break
            case "L":
                cell.matchCollectionLabel!.text = "Loss"
                cell.matchCollectionLabel!.textColor = UIColor.redColor()
                break
            default:
                cell.matchCollectionLabel!.textColor = UIColor.blackColor()
                break
            }
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            break
        default:
            if indexPath.section%2 == 0 {
                cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
            }
            cell.matchCollectionLabel!.text = ""
            break
        }
    }
    return cell
}

暂无
暂无

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

相关问题 UICollectionView.reloadData或UICollectionView.deleteItemsAtIndexPaths使单元格隐藏但仍可单击 - UICollectionView.reloadData or UICollectionView.deleteItemsAtIndexPaths makes cell hidden but still clickable UICollectionView.reloadData()不会触发UICollectionDataSource方法 - UICollectionView.reloadData() do NOT trigger UICollectionDataSource methods 调用UICollectionView.reloadData()是不是很糟糕? 如果是这样,为什么? - Is calling UICollectionView.reloadData() bad practice? If so, why? Swift CollectionView ReloadData 暂时改变顺序 - Swift CollectionView ReloadData changes order for a moment iOS UICollectionView特殊单元格顺序 - iOS UICollectionView special cell order UITableView 上的 iOS Swift ReloadData 不会刷新 UITableViewCell 内的 UICollectionView - iOS Swift ReloadData on UITableView does not refresh UICollectionView inside of UITableViewCell iOS 8 Swift UICollectionView reloadData()导致视图(图像)跳转单元格 - iOS 8 Swift UICollectionView reloadData() causing views (images) to jump cells UICollectionView.reloadData()在添加第一个项目后起作用,然后似乎在后面运行了一个 - UICollectionView.reloadData() works after adding first item, then seems to run one behind 迅速:uicollectionview在过渡期间更改单元格的contentoffset - swift: uicollectionview changes contentoffset of the cell during transition UICollectionView reloadData 在 iOS 7 中无法正常运行 - UICollectionView reloadData not functioning properly in iOS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM