开发者社区> 问答> 正文

UIPickerView只填充“?”

因此,我试图从数组中填充两个选择器视图,但只想出“?”我在这里尝试过一些类似类型的修复,但是没有一个成功。

@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
    }
}

}

我已经花了一天半的时间来解决这个问题,还没有弄清楚为什么它不能工作,我已经尝试了几个修复方法。

展开
收起
游客5akardh5cojhg 2019-12-05 16:28:07 391 0
1 条回答
写回答
取消 提交回答
  • 您需要将最底层的私有方法更改为:

    // The data to return fopr the row and component (column) that's being passed in
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            let startView: Int = startPickerData[row]
            let endView: Int = endPickerData[row]
            if pickerView.tag == 1 {
                return String(format: "%d", startView)
            } else {
                return String(format: "%d", endView)
            }
        }
    
    2019-12-05 16:28:45
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载