UICollectionView Header 标题更改

UICollectionView Header title change

我有一个 UICollectionView 有 4 个自定义 UICollectionViewCells。在 UICollectionView 的 header 中有一个 UISegmentedControl。我的目标是改变起标题作用的header UILabel。现在,如果分段控件值已更改,将重新加载单元格并且应切换标题,但它与第一​​个标题重叠。我不明白为什么。

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionElementKindSectionHeader {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderDiscoverVC", for: indexPath) as! HeaderDiscoverVC
        headerView.frame = CGRect(x: 0, y: 0, width: collectionView.frame.width, height: 30)
        headerView.backgroundColor = UIColor.hex("d9e2e7")
        let label = UILabel(frame: CGRect(x: 16, y: 0, width: headerView.frame.width, height: 30))

        switch segReusableIdentifier {
        case "Reply":
            label.text = "Reply"
        case "Media":
            label.text = "Media"
        case "Likes":
            label.text = "Likes"
        case "Comments":
            label.text = "Comments"
        default:
            label.text = ""
        }
        label.font = UIFont(name: Fonts.OpenSans_Bold, size: 16)
        label.textColor = UIColor.hex("8a9da6")
        headerView.addSubview(label)
        return headerView
    }

   fatalError("Unexpected element kind")
} 

问题在于您将标签添加到页眉视图的方式。

您应该将 headerView.addSubview(标签)放入您的 HeaderDiscoverVC Class。同时将颜色和字体设置为相同的 class.

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderDiscoverVC", for: indexPath) as! HeaderDiscoverVC
    headerView.frame = CGRect(x: 0, y: 0, width: collectionView.frame.width, height: 30)

    // MOVE THE COMMENTED LINE TO YOUR HeaderDiscoverVC
    //headerView.backgroundColor = UIColor.hex("d9e2e7")
    headerView.label.frame = CGRect(x: 16, y: 0, width: headerView.frame.width, height: 30)

    switch segReusableIdentifier {
    case "Reply":
        headerView.label.text = "Reply"
    case "Media":
        headerView.label.text = "Media"
    case "Likes":
        headerView.label.text = "Likes"
    case "Comments":
        headerView.label.text = "Comments"
    default:
        headerView.label.text = ""
    }

    // MOVE THE COMMENTED LINES TO YOUR HeaderDiscoverVC
    //label.font = UIFont(name: Fonts.OpenSans_Bold, size: 16)
    //label.textColor = UIColor.hex("8a9da6")
    //headerView.addSubview(label)
    return headerView
}

fatalError("Unexpected element kind")
} 

尝试并分享您的结果

您正在以编程方式将 label 添加到 headerView 中,在再次添加之前应将其删除。 dequeueReusableSupplementaryView 不要删除以编程方式添加的子视图。

在您的代码中:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionElementKindSectionHeader {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderDiscoverVC", for: indexPath) as! HeaderDiscoverVC
        headerView.frame = CGRect(x: 0, y: 0, width: collectionView.frame.width, height: 30)
        headerView.backgroundColor = UIColor.hex("d9e2e7")

        ///// Add below code to remove all subviews first before adding any new subview programmatically 
        for label in headerView.subviews {
            if let mylabel = label as? UILabel {
                mylabel.removeFromSuperview()
            }
        }

        ////////////////
        let label = UILabel(frame: CGRect(x: 16, y: 0, width: headerView.frame.width, height: 30))

        switch segReusableIdentifier {
        case "Reply":
            label.text = "Reply"
        case "Media":
            label.text = "Media"
        case "Likes":
            label.text = "Likes"
        case "Comments":
            label.text = "Comments"
        default:
            label.text = ""
        }
        label.font = UIFont(name: Fonts.OpenSans_Bold, size: 16)
        label.textColor = UIColor.hex("8a9da6")
        headerView.addSubview(label)
        return headerView
    }

   fatalError("Unexpected element kind")
} 

更好的方法是将 label 保留在 HeaderDiscoverVC 中并在代码中将其用作:

 headerView.label.text = "Your data"

这样您就不必以编程方式删除 subViews