gpt4 book ai didi

ios - 重用 UITableViewCell 时我的数据不会保留 - SWIFT 3

转载 作者:行者123 更新时间:2023-11-30 11:56:10 25 4
gpt4 key购买 nike

当我向下滚动 TableView 时遇到一些困难,单元格中的数据正在消失。如何在我的数据源中保留它们以使其更加具体?

提及我的单元格内部有一个 Collection View ,并且其中的项目显示错误。

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
}

}
}

这将是我的 collectionViewItem 以及我填充的数据。

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)
}
}

至于我的MainTableView =>

     // 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 和 Delegate 应位于包含 collectionViewViewController 中,单元格应仅包含您的 UIViews >,您正在单元格本身中设置单元格内容,tableViewcollectionView将重新加载单元格,并且您将丢失数据,而您应该在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
}

关于ios - 重用 UITableViewCell 时我的数据不会保留 - SWIFT 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47789427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com