Reputation: 127
I have a view controller that populates a tableView with Course venue and date fields. I am trying to get the selected row to pass the record to the 2nd view controller where I would like to display the venue title.
the code show no errors, except the UILabel for the venueTitle in the 2nd view controller does not display the venues title.
1st ViewController -
import UIKit
import CloudKit
class CoursesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var coursesTable: UITableView!
var coursesArray: Array<CKRecord> = []
var selectedCourseIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
coursesTable.delegate = self
coursesTable.dataSource = self
fetchCourses()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coursesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath)
let courseRecord: CKRecord = coursesArray[indexPath.row]
cell.textLabel?.text = courseRecord.value(forKey: "courseVenue") as? String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy"
let CSDate = dateFormatter.string(from: courseRecord.value(forKey: "courseDate") as! Date)
cell.detailTextLabel?.text = CSDate
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedCourseIndex = indexPath.row
self.performSegue(withIdentifier: "showMapDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapDetail" {
let coursesMap = segue.destination as! CourseMapVC
if let index = selectedCourseIndex {
coursesMap.courseRecord = coursesArray[index]
}
}
}
func fetchCourses() {
let container = CKContainer.default()
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(format: "TRUEPREDICATE")
let query = CKQuery(recordType: "Courses", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "courseDate", ascending: true)]
publicDatabase.perform(query, inZoneWith: nil) { (results, error) -> Void in
if error != nil {
print("error fetch notes \(error)")
} else {
print("Success")
for result in results! {
self.coursesArray.append(result )
}
OperationQueue.main.addOperation({ () -> Void in
self.coursesTable.reloadData()
self.coursesTable.isHidden = false
})
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
and the second view controller -
import UIKit
import CloudKit
import MapKit
class CourseMapVC: UIViewController {
@IBOutlet weak var venueTitle: UILabel!
@IBOutlet weak var mapView: MKMapView!
var courseRecord: CKRecord!
override func viewDidLoad() {
super.viewDidLoad()
if let course = courseRecord {
venueTitle.text = course.value(forKey: "courseTitle") as? String
}
let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
}
}
I am not worried about the CLLocation as yet, I am more concerned about information being passed or at this stage not. Any assistance greatly appreciated.
Upvotes: 0
Views: 88
Reputation: 127
Well a fresh pair of eyes (and brain) this morning and I have sorted out the Segue problem.
I needed to 1. Call the Array again (having done so for the tableView) 2. Get the field courseVenue from the selected row 3. Specify that the value was passed as a String
Thanks for all the pointers for this, here is the amended code.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapDetail" {
let destinationController = segue.destination as! CourseMapVC
if let indexPath = tableView.indexPathForSelectedRow {
let courses: CKRecord = coursesArray[indexPath.row]
let coursesVenueCell = courses.value(forKey: "courseVenue")
destinationController.courseTitle = coursesVenueCell as! String
}
}
}
Upvotes: 0
Reputation: 2588
Sounds like it's the UI layout problem not Storyboard. Have you tried to debug your view yet? The label may be clipped
Upvotes: 1
Reputation: 11539
Your segue identifier might be incorrect. Try using the optional cast of the segue destination controller to CourseMapVC
as the condition instead.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let controller as CourseMapVC:
if let index = selectedCourseIndex {
controller.courseRecord = coursesArray[index]
}
default:
break
}
}
Upvotes: 0