Перезагрузка видимой uicollectionviewcell при вложенности в uitableviewcell
У меня есть UICollection, который показывает блокировку на ячейках, которые заблокированы для пользователей, которые не вошли в систему. Пользователь может просматривать коллекцию, а затем входить в модальный. Когда модальное отклонение, я пытаюсь перезагрузить ячейки таблицы и вложенную коллекцию, чтобы удалить навесные замки из ячеек.
Видимые ячейки не освежают, чтобы удалить замок. Когда прокрутка коллекции, ячейки вне экрана правильные и показывают с помощью висячего замка. Я вызываю reloaddata() как в представлении tableview, так и в каждом вложенном представлении коллекции.
Код, который у меня есть, разделяется на:
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
}
}
Ответы
Ответ 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
}
Ответ 2
Проблема заключается в том, что:
tableViewCell владеет данными, которые используются для настройки коллекцииViewCell.
Если вы хотите, чтобы ваш collectionViewCell обновлялся без необходимости tableView.reloadData, данные, которые используются для настройки коллекцииViewView (в вашем случае "тренировка" ), должны быть извлечены из другого места, кроме как из tableViewCell.