簡體   English   中英

UICollectionView.reloadData()更改單元格順序| iOS Swift

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

因此,我正在使用一個簡單的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
}

我使用的自定義單元MatchCollectionViewCell僅包含一個標簽matchCollectionLabel,就是這樣。 我在視圖的viewDidAppear函數中使用self.matchCollectionView.reloadData()來查看輔助視圖何時更改表並將其退回到原始視圖。

第一次使用后,我會標記單元格,因為我不會在每個viewDidAppear之后不斷向單元格中添加不必要的子視圖。

初始設置后,該表看起來不錯,但是在調用reloadData之后,collectionView的單元格順序被更改並混亂。 如果我編輯單元格數,則reloadData()更新以正確顯示單元格數更改,但是單元格的順序仍然不正確。

這是reloadData之前和之后的圖片

http://imgur.com/wqfXqLG.png

任何幫助將不勝感激! 謝謝!

您每次都在創建一個新標簽。 如果標記不是19,則僅將其添加為子視圖,但始終分配新標簽。

這意味着,當您重復使用單元格時,您將更新一個新標簽,而不是已添加到該單元格的標簽,因此您的更改不可見。

您可以將標記檢查移至函數頂部,並使用它來控制標簽的分配,或者將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM