I am having trouble passing data from the selected row in my TableView to the other ViewController.
class SectionsTableViewController: UITableViewController {
var sections: [Sections] = SectionsData().getSectionsFromData()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sections[section].items.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].headings
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "sectionCell", for: indexPath)
// Configure the cell...
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "sectionCell")
{
let upcoming: Sjekkliste = segue.destination as! Sjekkliste
let indexPath = self.tableView.indexPathForSelectedRow!
let titleString = Sections(title: "", objects: [""]) as? String
upcoming.titleString = titleString
self.tableView.deselectRow(at: indexPath, animated: true)
}
This is where my problem is: let titleString = Sections(title: "", objects: [""]) as? String
let titleString = Sections(title: "", objects: [""]) as? String
It would be preferred if the title and objects was passed separately.
This is my data setup:
class SectionsData {
var myArray: [AnyObject] = []
func getSectionsFromData() -> [Sections] {
var sectionsArray = [Sections]()
let Generell = Sections(title: "Animals", objects:
["Cat", "Dog", "Lion", "Tiger"])
sectionsArray.append(Generell)
return sectionsArray
This line suggests you subclassed String into Sections. Why are you subclassing String? If Sections is not a subclass of string then this line will fail.
let titleString = Sections(title: "", objects: [""]) as? String
I'm assuming you want something similar to this:
let selectedSection:Sections = Sections(title: "", objects: [""])
upcoming.titleString = selectedSection.title
The above suggests a "title" property on the Sections object is what you are looking to set as the "titleString" property of the next view controller.
Unrelated to the original question you should lowercase "Generell". Best practices suggests only class names should start with a capital letter. I also suggest implementing the didSelectRow method to pick your data. The results should looks similar to this:
var selectedSection:Sections? //goes at top
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
self.selectedSection = self.sections[indexPath.section]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "sectionCell"){
let upcoming: Sjekkliste = segue.destination as! Sjekkliste
if let section = self.selectedSection{
upcoming.titleString = section.title
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.