我是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]
    }


我有两种不同的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
}

最佳答案

nameFiles正在访问注释中的名称。

nameFiles[row]不起作用,因为nameFiles不是数组。

如果要返回字符串,只需执行return nameFiles。不过,请首先检查以确保nameFiles实际上具有值!

10-08 14:00