ios - 嵌套在 uitableviewcell 中时重新加载可见的 uicollectionviewcell

标签 ios swift uitableview uicollectionviewcell

我有一个 UICollection,它在锁定给未登录用户的单元格上显示挂锁。用户可以查看集合,然后以模式登录。当模态消失时,我试图重新加载表格的单元格和嵌套集合以从单元格中删除挂锁。

可见单元格未刷新以移除挂锁。滚动集合时,屏幕外的单元格是正确的并显示为挂锁。我在 tableview 和每个嵌套的 collectionview 上调用 reloaddata()。

我的代码分为:

UIViewController

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("SectionWorkouts", forIndexPath: indexPath) as! SectionTableViewCell

        cell.delegate = self

        // Return the workouts count from the section
        let intIndex = indexPath.row

        let index = workoutSections.startIndex.advancedBy(intIndex)

        let currentWorkoutSectionKey = workoutSections.keys[index]

        if let currentWorkoutSection = workoutSections[currentWorkoutSectionKey] {

            cell.workoutsCollection.dataSource = sectionWorkoutsCell
            cell.workoutsCollection.delegate = sectionWorkoutsCell

            cell.updateCellWithWorkouts(currentWorkoutSectionKey, workouts: currentWorkoutSection)


        }

    }

    return cell
}

UITableViewCell

class SectionTableViewCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource {

    var workouts = [Workout]()
    var delegate: WorkoutCellDelegate?

    @IBOutlet weak var sectionTitle: UILabel!
    @IBOutlet weak var workoutsCollection: UICollectionView!

    func updateCellWithWorkouts(title: String, workouts: [Workout]){

        self.sectionTitle.text = title
        self.workouts = workouts
        dispatch_async(dispatch_get_main_queue(),{
            self.workoutsCollection.reloadData()
        })

    }


    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SectionWorkoutCell", forIndexPath: indexPath) as! SectionCollectionViewCell

        let row = indexPath.row

        let workout = workouts[row]

        cell.configCell(workout)

        return cell

    }


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return workouts.count
    }


    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let row = indexPath.row
        let feature = workouts[row]

        if let delegate = self.delegate{
            delegate.didSelectWorkoutCell(feature)
        }

    }

}

UICollectionViewCell

class SectionCollectionViewCell: UICollectionViewCell {

 @IBOutlet weak var imageContainer: UIView!
 @IBOutlet weak var image: UIImageView!
 @IBOutlet weak var tintOverlay: UIView!
 @IBOutlet weak var padlock: UIImageView!

 @IBOutlet weak var workoutTitle: UILabel!
 @IBOutlet weak var duration: UILabel!

 var locked = true

  required init?(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)

  }


  func configCell(workout: Workout){

    self.workoutTitle.text = workout.name

    if workout.type == "Free" || AccountManager.userIsLoggedInMember() {
        self.setToUnLocked()
    }else{
        self.setToLocked()
    }

    self.layoutIfNeeded()

  }


  func setToUnLocked(){
    locked = false
    tintOverlay.alpha = 0
    padlock.alpha = 0
  }


  func setToLocked(){
    locked = true
    tintOverlay.alpha = 0.6
    padlock.alpha = 1
  }

}

最佳答案

您可能应该将配置单元格的调用移至 willDisplayCell: 方法。您可以从 cellForItemAtIndexPath 方法中删除它。这是配置要显示的单元格的任何视觉方面的正确时间。

func collectionView(collectionView: UICollectionView,
    willDisplayCell cell: UICollectionViewCell,
    forItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let row = indexPath.row

    let workout = workouts[row]

    cell.configCell(workout)

    return cell

}

关于ios - 嵌套在 uitableviewcell 中时重新加载可见的 uicollectionviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884038/

相关文章:

ios - 编辑框架高度集合可重用 View

iOS 7后台上传和POST请求

ios - 将视频保存到相机胶卷时出错? [ swift ]

swift - 如何从网站获取图像源 - Swift & Kanna

ios - 如何在 UITableViewCell 中添加交互式 View Controller ?

ios - 如何以编程方式更改 UITableView 的类?

ios - 如何覆盖 Alamofire SessionManager 以修改响应以添加 header ?

ios - 检查 Objective-C 字符串中的特定字符

ios - 触摸在Xcode模拟器中无法工作(苹果官方教程 'FoodTracker')

iphone - 如何获得像 Phone.app -"Recents tab"或 Mail.app -"mail list"这样的 TableViewCell