首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >嵌套在uitableviewcell中时重新加载可见uicollectionviewcell

嵌套在uitableviewcell中时重新加载可见uicollectionviewcell
EN

Stack Overflow用户
提问于 2016-08-11 05:41:11
回答 1查看 2.2K关注 0票数 20

我有一个UICollection,它显示单元格上的挂锁,锁定到未登录的用户。用户可以查看集合,然后以模式登录。当模式关闭时,我尝试重新加载表格和嵌套集合的单元格,以从单元格中移除挂锁。

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

我的代码被分成:

UIViewController

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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
  }

}
EN

回答 1

Stack Overflow用户

发布于 2016-08-19 00:40:49

您可能应该将配置单元的调用转移到willDisplayCell:方法。您可以将其从cellForItemAtIndexPath方法中删除。现在是配置要显示的单元格的任何可视方面的正确时机。

代码语言:javascript
复制
func collectionView(collectionView: UICollectionView,
    willDisplayCell cell: UICollectionViewCell,
    forItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let row = indexPath.row

    let workout = workouts[row]

    cell.configCell(workout)

    return cell

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38884038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档