我使用UITableViewCell.xib定制单元格。我显示文本和图片,将View添加到单元格的背景中。只有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
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell
if indexPath.row == 3 {
// here i'm doing something wrong because nothing changes
cell?. customBackgoundView?.addSubview(CollectionViewCell())
}
return cell ?? UICollectionViewCell()
}
您需要按以下方式在indexPath.row == 3上加载UIView
Bundle.loadView(fromNib:"yourView", withType: yourView.self)
然后添加到收藏夹视图的背景主视图中