【发布时间】: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