我是新來swift,我想重新填充pickerview使用另一個類的名稱數組。我有這個類創建筆記的筆記,每個筆記都有一個名稱/標題,我試圖訪問這些名稱/標題並將它們放在pickerview中,任何人都可以幫助我。從另一個viewController訪問信息
import UIKit
var allNotes: [Note] = []
var currentNoteindex: Int = -1
var noteTable: UITableView?
let kAllNotes:String = "notes"
class Note: NSObject
{
var date:String
var note:String
var name:String
override init()
{
date = NSDate().description
name = ""
note = ""
}
func dictionary() -> NSDictionary
{
return ["note": note, "date": date, "name": name]
}
我有兩個不同的pickerview和一個作品,它只是一個名稱不起作用。
override func viewDidLoad()
{
super.viewDidLoad()
fileNamePicker.dataSource = self
fileNamePicker.delegate = self
gradePicker.dataSource = self
gradePicker.delegate = self
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if pickerView == gradePicker
{
return grades.count
}
else
{
return allNotes.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if pickerView == gradePicker
{
return grades[row]
}
else
{
//This is where the Problem is, but I don't know how to fix it.
let nameFiles = allNotes[currentNoteindex].name
return nameFiles[row]
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
'let nameFiles = allNotes [currentNoteindex] .name'已經是'String'。你爲什麼不只是返回var? – Ryan
是的,我試過,但它不起作用。 –
你是什麼意思'不起作用?你能在這裏添加更多細節嗎? – Ryan