Ios UIPickerView只填充“是吗?”&引用;

Ios UIPickerView只填充“是吗?”&引用;,ios,swift,Ios,Swift,因此,我试图从数组中填充我的两个PickerView,但只提出了“?”我在这里尝试了一些类似类型的修复,但没有一个有效 @IBOutlet var startPickerView: UIPickerView! @IBOutlet var endPickerView: UIPickerView! var startPickerData: [Int] = [] var endPickerData: [Int] = [] let startArray = (1.

因此,我试图从数组中填充我的两个PickerView,但只提出了“?”我在这里尝试了一些类似类型的修复,但没有一个有效

    @IBOutlet var startPickerView: UIPickerView!
    @IBOutlet var endPickerView: UIPickerView!
    var startPickerData: [Int] = []
    var endPickerData: [Int] = []
    let startArray = (1...999).map { $0 }
    let endArray = (2...1000).map { $0 }

    let startPickerCellIndexPath = IndexPath(row: 1, section: 1)
    let endPickerCellIndexPath = IndexPath(row: 1, section: 2)

    var isStartPickerShown: Bool = false {
        didSet {
            startPickerView.isHidden = !isStartPickerShown
        }
    }
    var isEndPickerShown: Bool = false {
        didSet {
            endPickerView.isHidden = !isEndPickerShown
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.startPickerView.delegate = self
        self.startPickerView.dataSource = self
        self.endPickerView.delegate = self
        self.endPickerView.dataSource = self
        startPickerData = startArray
        endPickerData = endArray

        self.startPickerView.reloadAllComponents()
        self.endPickerView.reloadAllComponents()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath {
        case startPickerCellIndexPath:
            if isStartPickerShown {
                return 216.0
            } else {
                return 0.0
            }
        case endPickerCellIndexPath:
            if isEndPickerShown {
                return 216.0
            } else {
                return 0.0
            }
            default:
            return 44.0
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        switch indexPath {
        case startPickerCellIndexPath:

            if isStartPickerShown {
                isStartPickerShown = false
            } else if isStartPickerShown {
                isStartPickerShown = false
                isStartPickerShown = true
            } else {
                isStartPickerShown = true
            }

            tableView.beginUpdates()
            tableView.endUpdates()

        case endPickerCellIndexPath:
            if isEndPickerShown {
                isEndPickerShown = false
            } else if isEndPickerShown {
                isEndPickerShown = false
                isEndPickerShown = true
            } else {
                isEndPickerShown = true
            }

            tableView.beginUpdates()
            tableView.endUpdates()  
            default:
            break
        }
    }


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }


    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView.tag == 1 {
            return startPickerData.count
        } else {
            return endPickerData.count
        }
    }

    // The data to return fopr the row and component (column) that's being passed in
    private func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int? {
        let startView: Int = startPickerData[row]
        let endView: Int = endPickerData[row]

        if pickerView.tag == 1 {
        return startView
        } else {
        return endView
        }
    }
}


我已经为此工作了一天半,还没有弄明白为什么它不起作用,我已经尝试了一些修复方法

您需要将底部的私有方法更改为:

//返回正在传入的行和组件(列)的数据
func pickerView(pickerView:UIPickerView,titleForRow行:Int,forComponent组件:Int)->String?{
让startView:Int=startPickerData[行]
let endView:Int=endPickerData[行]
如果pickerView.tag==1{
返回字符串(格式:“%d”,startView)
}否则{
返回字符串(格式:“%d”,endView)
}
}

这就成功了,非常感谢!只是pickerView不会显示Int dat类型吗?没问题!
UIPickerViewDelegate
协议要求该方法返回
String?
类型,因此必须将
Int
值解析为
String
。请随时更新我的答案,并尽快将其标记为正确。我更新了答案,但它不会显示,因为我的声誉为0,并且标记为正确。太好了!欢迎来到StackOverflow