[英]TableView array segue won't pass data
我在将数据从TableView中的选定行传递到另一个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)
}
这是我的问题所在: let titleString = Sections(title: "", objects: [""]) as? String
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细分为Sections。 为什么要继承String? 如果Sections不是字符串的子类,则此行将失败。
let titleString = Sections(title: "", objects: [""]) as? String
我假设您想要与此类似的东西:
let selectedSection:Sections = Sections(title: "", objects: [""])
upcoming.titleString = selectedSection.title
上面的建议在Sections对象上的“ title”属性是您要设置为下一个视图控制器的“ titleString”属性的内容。
与原始问题无关,您应该将“ Generell”小写。 最佳做法建议仅班级名称应以大写字母开头。 我还建议实现didSelectRow方法来选择您的数据。 结果应类似于以下内容:
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.