Вот отсюда:
class AppointmentsVC: UIViewController {
var appointments: [Appointment] = []
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
fetchDataForTableView()
}
// MARK: - Fetch Data
private func fetchDataForTableView() {
NetworkManager.fetchAllAppointments { (appointmetns) in
self.appointments = appointmetns
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let cell = CustomCellForAppointmentsVC()
if segue.identifier == "goToRatingModal" {
// на всякий случай попробую еще так
guard let indexPath = tableView.indexPathForSelectedRow else { return }
guard let dvc = segue.destination as? ModalRatingVC else { return }
dvc.doctorID = appointments[indexPath.row].doctor.id
}
}
}
extension AppointmentsVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return appointments.count + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "makeAppointment", for: indexPath) as? CustomCellForAppointmentsVC
return cell ?? UITableViewCell()
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "appointmentCell", for: indexPath) as! CustomCellForAppointmentsVC
let appointment = appointments[indexPath.row - 1]
cell.date.text = "\(appointment.date) \(appointment.time_start)"
cell.nameOfClinic.text = appointment.filial.name
cell.placeOfClinic.text = appointment.filial.address
cell.doctorName.text = appointment.doctor.name
cell.doctorSpecialize.text = appointment.doctor.specialty
cell.leaveRatingButton.addTarget(self, action: #selector(setNumberFromArray), for: .touchUpInside)
cell.leaveRatingButton.tag = indexPath.row
cell.secondCellView.setupViewForBonusScreen()
NetworkManager.fetchImage(path: appointment.filial.image ?? "1599117490.jpeg") { (imageData) in
cell.imageOfClinic.image = UIImage(data: imageData)
}
NetworkManager.fetchImage(path: appointment.doctor.photo ?? "1599117490.jpeg") { (imageData) in
cell.imageOfDoctor.image = UIImage(data: imageData)
}
return cell
}
}
@objc private func setNumberFromArray(_ button: UIButton) {
let buttonTag = button.tag
let vc = ModalRatingVC()
vc.doctorID = buttonTag
self.navigationController?.pushViewController(vc, animated: false)
}
}
…нужно передать doctorID сюда:
class ModalRatingVC: UIViewController {
var doctorID: Int?
@IBOutlet var ratingStackView: RatingControl!
@IBOutlet var commentTF: UITextField!
@IBOutlet var publishButton: UIButton!
@IBOutlet var ratingView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
print(doctorID)
}
private func setupViews() {
ratingView.setupViewForBonusScreen()
publishButton.setupPinkButton()
commentTF.setupCustomTF()
}
@IBAction func closeModal(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
@IBAction func publishRating(_ sender: UIButton) {
guard let doctorID = doctorID, let comment = commentTF.text else { return }
let body: [String: Any] = ["rate": ratingStackView.rating,
"text": comment,
"user_id": 123,
"doctor_id": doctorID]
NetworkManager.publishRating(body: body) {
// close modal
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
}