【问题标题】:Add background view in collection cell在集合单元格中添加背景视图
【发布时间】:2020-04-21 12:54:15
【问题描述】:

我使用 UITableViewCell.xib 来自定义单元格。我显示文本和图片,视图添加到单元格的背景。只有 7 个单元格,在一个单元格中我需要用另一个 UIView.xib 替换这个背景。我试图实现这一点,但它没有给我结果,所有单元格的背景都是相同的。 或者如何将背景视图单元格的属性更改为我的 View.xib

CollectionCell.swift

import UIKit

class CollectionViewCell: UICollectionViewCell {

    static var identifier: String = "CollectionViewCell"

    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var customBackgoundView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

    }
}

CustomeView.swift

import UIKit

class CustomBackgroundView: UIView {

    @IBOutlet weak var contentView:UIView!


    //MARK: Override
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        Bundle.main.loadNibNamed("CustomBackgroundView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
    }
}

CollectionView.swift 让背景 = CustomBackgroundView()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell


            // here i'm doing something wrong because nothing changes
  if indexPath.row == 3 {
        cell?.customBackgoundView = background
        cell?.contentView.addSubview(background)
    }
        return cell ?? UICollectionViewCell()
    }

【问题讨论】:

    标签: ios swift view uicollectionview


    【解决方案1】:

    问题是当你想在单元格上添加一些东西时,它应该添加它的内容视图。所以,尝试使用 contentView

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell
     if indexPath.row == 3 {
                     var backgroundView = Bundle.main.loadNibNamed("CustomBackgroundView", owner: self, options: nil)![0] as! UIView
                 backgroundView.frame = cell.contentView.frame
                cell.contentView.addSubview(yourCustomView)
                cell.contentView.sendSubviewToBack(yourCustomView)
            return cell!
                    } else {
                        return cell ?? UICollectionViewCell()
                    }
    

    【讨论】:

    • 我不太明白你的代码,你能更详细地展示一下吗?我更新了上一课的问题和代码
    • 我已经编辑了我的答案,现在您可能会有所了解。
    • 我不需要在单元格上添加。该单元格有一个来自 UIView (customBackgroundView) 的出口。这个插座需要分配我所做的视图。像这样的东西。不幸的是,您的代码也没有改变任何东西;白色背景保持不变。查看
    【解决方案2】:

    您需要在 indexPath.row == 3 上加载 UIView,如下所示

    let myView = Bundle.main.loadNibNamed("yourXibView", owner: nil, options: nil)![0] as! UIView
    

    然后添加您的收藏视图的背景主视图

    【讨论】:

    • 说明没有这个方法,不太明白怎么写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2014-08-18
    • 2022-12-20
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多