[英]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.