Я использую Core Data и хочу отобразить только один раздел массива, передав из предыдущего View Controller строку одного из заголовков разделов. Я также использую Core Data, поэтому я пытаюсь выяснить, как я могу установить раздел по строке, а затем показать объекты для этого раздела.
Я пробовал следующее:
Сначала я поместил заголовок раздела в секцию, озаглавленную selectedSection.
Затем я ставлю число разделов как 1, так как будет только 1.
Затем я устанавливаю titleForHeaderInSection как возвращение selectedString.
Где все идет не так, как надо в numberOfRowsInSection. Я не могу понять, как установить раздел для selectedString. Я выложу код, который я попробовал ниже.
// From Previous View Controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "ShowSection" {
let controller = segue.destination as! SectionsViewController
controller.context = context
let button = sender as! UIButton
let section = photos[button.tag].place
controller.selectedSection = section!
// This passes on a String
}
}
// Saving pins to the map
if let annotationView = annotationView {
annotationView.annotation = annotation
let button = annotationView.rightCalloutAccessoryView as!
if let index = photos.firstIndex(of: annotation as! Photo) {
button.tag = index
}
// In the Sections View Controller (i.e. the second VC)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedRC.sections {
let currentSection = sections[section]
return currentSection.numberOfObjects
} else {
return 0
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return selectedSection
}
Любая идея, как я могу установить раздел как selectedSection и показать объекты для выбранного selectedSection? Большое спасибо заранее!