【问题标题】:My data isn't retained when the UITableViewCell is reuse - SWIFT 3重用 UITableViewCell 时我的数据没有保留 - SWIFT 3
【发布时间】:2017-12-13 09:19:23
【问题描述】:

当我向下滚动 TableView 时遇到了一些困难,我的单元格中的数据正在消失。我怎样才能让它们在我的数据源中更具体?

提到我的单元格内部有一个集合视图,并且其中的项目显示不好。

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
            } 

【问题讨论】:

  • 你应该在cellForRowAtIndexpath方法中设置值。
  • 我的回答解决了你的问题吗?

标签: ios objective-c swift uitableview uicollectionview


【解决方案1】:

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
}

【讨论】:

  • 嘿。感谢您的回复。顺便说一句,标签在哪里?使用未解析的标识符“标签”。请解释一下您希望我替换什么标签?
  • Label 是您要为每个单元格显示的项目数组,对于每个 priceCutLabel 将有一个这些项目的数组,priceCutArray = [23,44,66] 所以你的 cell.priceCutLabel.text = priceCutArray[indexPath.row]
  • 好吧,这没有帮助。问题是所有数据都来自 cell.kid = package?.kids[indexPath.row] 其中 child 设置在 collectionViewCell
  • 当我在滚动后重用单元​​格时,我需要一个模型来保留我的数据。当我选择另一个 tableViewCell 时,它也会弄乱我的数据
  • 您的单元格数据需要在tableView delegate 中,为了能够在每个单元格内单独显示数据,您需要将单元格作为项目的占位符,然后更新单元格标签使用cellForRowatindexPath,所以在tableView ViewController 中从标签中定义一些数组并设置单元格,你不能更新它自己的单元格内的数据,因为行方法的单元格将继续更新单元格和你的数据将丢失。删除cell.kid = package?.kids[indexPath.row]
猜你喜欢
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2016-09-20
  • 1970-01-01
  • 2022-07-02
  • 1970-01-01
相关资源
最近更新 更多