2
我無法將數據從TableView中的選定行傳遞到另一個ViewController。TableView數組segue不會傳遞數據
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)
}
這是我的問題是:let titleString = Sections(title: "", objects: [""]) as? String
如果標題和對象是分別通過這將是首選。
這是我的數據設置:
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
'String'是一個結構,因此不能被繼承 – Sweeper
哎呀......我在想的NSString:/ –