我是swift的新手,我正在尝试使用另一个类的名称数组重新填充pickerview。我有这个类Notes创建笔记,每个笔记都有一个名称/标题,我试图访问这些名称/标题并将它们放在pickerview中,任何人都可以帮助我。
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]
}
我有两个不同的选择视图,一个工作,只是名称不起作用的那个。
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
}
答案 0 :(得分:0)
nameFiles
正在从记事中访问姓名。
nameFiles[row]
无法正常工作,因为nameFiles
不是数组。
如果要返回字符串,只需执行return nameFiles
即可。首先检查以确保nameFiles
实际上有值!
答案 1 :(得分:0)
看起来你的allNotes数组是空的。你有initilaize但似乎没有追加对象。在initiliaze
类
Note
方法
init(_ date: String, note: String, name: String) {
self.date = date
self.note = note
self.name = name
}
在你的viewcontroller类中尝试像这样追加。现在,您可以访问内部选择器视图方法: -
//You can append by doinf iteration based on your data
allNotes.append(Note(date: "value1", note: "Value", name: "name"))