当我向下滚动桌面时,我遇到了一些困难,单元格中的数据正在消失。我如何保留它们在我的数据源中更具体的?
提到我的单元格内部有收集视图,其中的项目显示不好。
class PromoTowerCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var multiplierNumberLabel: UILabel!
@IBOutlet weak var iconPromo: UIImageView!
@IBOutlet weak var namePromoLabel: UILabel!
@IBOutlet weak var priceCutLabel: UILabel!
@IBOutlet weak var priceActualLabel: UILabel!
@IBOutlet weak var infoPopUp: MyButton!
// MARK: - Setters
var kid : PromoPackageKid? {
didSet {
self.namePromoLabel.text = kid?.name
self.multiplierNumberLabel.text = kid?.power
self.priceActualLabel.text = String(describing: kid!.prices[Constants.CARD_PAYMENT]!) + " €"
self.infoPopUp.promoText = kid?.texts
if kid?.prices[Constants.CARD_PAYMENT] != kid?.commonPrices[Constants.CARD_PAYMENT] {
let stringPrice = String(describing:kid!.commonPrices[Constants.CARD_PAYMENT]!) + " €"
let attributeString = NSMutableAttributedString(string: stringPrice)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))
self.priceCutLabel.attributedText = attributeString
} else {
self.priceCutLabel.isHidden = true
}
}
}
那将是我的收藏viewitem,其中包含我填充它的数据。
class PromoTowerTableViewCell: UITableViewCell {
var package : PromoPackage? {
didSet {
collectionView.reloadData()
}
}
@IBOutlet weak var collectionView: PromoTowerCollectionView!
// MARK: - Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib(nibName: "PromoTowerCollectionCell", bundle: nil), forCellWithReuseIdentifier: "PromoTowerCollectionViewCell")
collectionView.reloadData()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
// MARK: DataSource
extension PromoTowerTableViewCell: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return package?.kids.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PromoTowerCollectionViewCell", for: indexPath) as! PromoTowerCollectionViewCell
cell.kid = package?.kids[indexPath.row]
cell.iconPromo.image = UIImage(named: iconImages[indexPath.row])
return cell
}
}
//MARK: Delegate
extension PromoTowerTableViewCell: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? PromoTowerCollectionViewCell {
cell.layer.borderWidth = 2.0
cell.layer.borderColor = Theme.defaultColor().cgColor
cell.layer.shadowOffset = CGSize.init(width: 0.5, height: 0.5)
}
}
至于我的维护=>
// MARK: - UITableViewDataSource + Delegate
func expandableCell(forSection section: Int, inTableView tableView: ExpyTableView) -> UITableViewCell {
let currentPackage = self.packages[section]
if section < self.packages.count {
if currentPackage.type == "bundle"{
let cell = tableView.dequeueReusableCell(withIdentifier: "PromoTowerCell") as! PromoTowerTableViewCell
cell.package = currentPackage
return cell
}
Datasource and Delegate
应在ViewController
中持有collectionView
,单元格仅应包含您的UIViews
,您将在单元格中设置单元格内容,即tableView
或collectionView
将重新加载单元格和您将丢失数据,而应该在cellForRowAtIndexpath
中设置值,而您遵循的方法确实很奇怪。
arrayOne = ["Item 1", "Item 2", "Item 3"]
arrayTwo = ["Item A", "Item B", "Item C"]
// MARK: Delegate & DataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayOne.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PromoTowerCollectionViewCell", for: indexPath) as! PromoTowerCollectionViewCell
cell.iconPromo.text = arrayOne[indexPath.row]
cell.namePromoLabel.text = arrayTwo[indexPath.row]
return cell
}