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