I have a problem with reload data in table view. My app structure has two container views, where the first container view is collection view controller and the second container view is table view controller. In collection view, I select the cell and in table view I want to show information. I need to reload all the data in table view because I can select any cell in the collection view.
In selectedDay
I get information about the cell I selected in didSet I make a request to Firebase (fetchDataFromFirebase
) and get information for the selected day. After that, I append data in workoutList
array and in didSet I need to reload all my table view.
My code in TableViewController
class ScheduleTableViewController: UITableViewController {
var selectedDay: Day? {
didSet {
workoutList.removeAll()
fetchDataFromFirebase(day: selectedDay?.day ?? "",
month: selectedDay?.month ?? "",
year: selectedDay?.year ?? "")
}
}
weak var delegate: EmptyDataInSelectedDayProtocol?
private var workoutList = [NewWorkout]() {
didSet {
print(workoutList)
print(workoutList.count)
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return workoutList.count
}
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ScheduleTableViewCell
cell.configure(in: workoutList[indexPath.row])
return cell
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let reSchedule = UIContextualAction(style: .normal, title: "Change workout day") { [unowned self] action, view, completionHandler in
workoutList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
}
reSchedule.image = UIImage(systemName: "arrow.up.arrow.down")
reSchedule.backgroundColor = .primary.withAlphaComponent(0.8)
return UISwipeActionsConfiguration(actions: [reSchedule])
}
private func fetchDataFromFirebase(day: String, month: String, year: String) {
FirebaseHelper.shared.getRefenceFirebase()
.child("\(FirebaseHelper.shared.getUserID())").observe(.value) { [self] snapshot in
DispatchQueue.main.async { [self] in
guard let value = snapshot.value as? [String:Any] else {return}
for info in value {
let data = value["\(info.key)"] as? NSDictionary
guard let userName = data?["userName"] as? String else {return}
guard let trainingStartDate = data?["trainingStartDate"] as? String else {return}
guard let trainingTime = data?["trainingTime"] as? String else {return}
guard let workoutDuration = data?["workoutDuration"] as? String else {return}
guard let selectedColor = data?["selectedColor"] as? [String:Any] else {return}
guard let workoutDates = data?["workoutDates"] as? [String] else {return}
guard let weekDays = data?["weekDays"] as? [String:Any] else {return}
for item in workoutDates {
if "\(day).\(month).\(year)" == item {
delegate?.isEmptyData(in: false)
let workout: [NewWorkout] = [NewWorkout(userName: userName,
weekDays: weekDays,
trainingStartDate: trainingStartDate,
workoutDuration: workoutDuration,
wortoutTime: trainingTime,
userColor: [UserColor(red: selectedColor["red"] as? Double ?? 0.0,
green: selectedColor["green"] as? Double ?? 0.0,
blue: selectedColor["blue"] as? Double ?? 0.0)],
workoutDates: workoutDates)]
workoutList.append(contentsOf: workout)
}
}
}
}
}
}
}