当我向下滚动TableView时,我遇到了一些困难,我单元格中的数据正在消失。如何在DataSource中保留它们更具体?
提到我的单元格里面有一个集合视图,其中的项目显示不好。
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
}
答案 0 :(得分:0)
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
}