I am working on a project and I am using RXSwift. I have a tableview and I have two different cells. Both cells are two different sizes. I tried putting in the heightForRow delegate func with an if else checking the two different cells and with a switch statement checking the two but it still displays the larger cell height for both cells only.
extension CalendarVC: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if workoutsTableView.cellForRow(at: indexPath) == FutureSessionCell() {
return 190
}else {
return 390
}
}
}
func bindTableViewWorkouts() {
vm.workoutForOneDay.asObservable()
.bind(to: workoutsTableView.rx.items) { (tv, row, workout) -> UITableViewCell in
if workout.createdByCoach {
let cellIdentifer = FutureSessionCell.identifier
let cell = tv.dequeueReusableCell(withIdentifier: cellIdentifer, for: IndexPath.init(row: row, section: 0)) as! FutureSessionCell
cell.initCell(userSettings: UserHandler.shared.user.settings, workout: workout, name: UserHandler.shared.user.name)
return cell
}else {
let cellIdentifer = WorkoutsCardCell.identifier
let cell = tv.dequeueReusableCell(withIdentifier: cellIdentifer, for: IndexPath.init(row: row, section: 0)) as! WorkoutsCardCell
cell.initCell(userSettings: UserHandler.shared.user.settings, workout: workout, name: UserHandler.shared.user.name)
cell.openWorkout = { [unowned self] workout in
workout.presentDetailsVC(self, homeTapBarController: self.tapBarController, retrieveWorkout: { [unowned self] newWorkout in
UserHandler.shared.user.updateWorkoutData(newWorkout: newWorkout)
self.vm.updateWorkoutsForSelectedDay(self.calendar.selectedDate ?? Date()) // need to reload tableView because we updated one existing element from inside array
}, backVC: 1)
}
return cell
}
}.disposed(by: disposeBag)
vm.workoutForOneDay.asObservable()
.bind(onNext: { newWorkouts in
self.noWorkoutsView.isHidden = !newWorkouts.isEmpty
}).disposed(by: disposeBag)
}
2
Answers
Try
I suggest you setup your cell constraints so the cells autoresize. If you must manually tell the table view the height of each cell, then I would do it this way:
In order for the above to work, you would need to follow my article on how to make an Rx Delegate Proxy. Which will produce this class (put the code here in its own file.)