Pickerview内容未显示

时间:2017-11-01 19:26:11

标签: ios swift string view uipickerview

我正在尝试获取一个包含数字列表的选择器视图。

到目前为止,我可以获取字符串来保存所有数字,但是当它将它放入选择器视图时,选择器视图始终为空。你能看一下我的代码,让我知道我哪里出错吗?

import Foundation
import UIKit

class PickerView: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    @IBOutlet weak var questionNumberPickers: UIPickerView!

    var Category: String!
    var numbDict: String!
    var QuestionNumber: String!
    var cell: UITableViewCell?
    var currentTopicIndex = Int()
    var setCount = Int()
    var setNumber = Int()
    var pickerString2 = [String]()
    var selectedQuestionNumber: String!
    var contacts: NSArray?
    var dictContacts = Int()
    var pickerArray: NSArray?

    @IBOutlet weak var numberOfRow: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        print(currentTopicIndex)

        questionNumberPickers.delegate = self
        questionNumberPickers.dataSource = self

        self.numberOfRow.text = "0"
        setCount = Quiz.quizzes[currentTopicIndex].plist.count

        let min = 1
        let max = setCount
        let intArray: [Int] = Array(min...max)
        print(intArray)

        let pickerString2 = intArray.map
        {
            String($0)
        }

        self.questionNumberPickers?.reloadAllComponents()

        print(pickerString2)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "questionToQuiz"){

            //   Get the new view controller using [segue destinationViewController].
            let detailScene = segue.destination as! QuizController


            detailScene.numberOfQuestions = numberOfRow.text as String!
        }
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerString2.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerString2[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        numberOfRow.text  = pickerString2[row]
    }
}

1 个答案:

答案 0 :(得分:1)

从let pickerString2 = intArray.map中删除“let”         {             字符串($ 0)         }

在viewDidLoad中,您正在初始化名为pickerString2的第二个变量。

<link href="https://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="map_container">
  <img src="http://geology.com/world/world-map-clickable.gif" />
  <a href="#" id="one" class="map-marker">
    <span aria-hidden="true">1</span>
  </a>
  <a href="#" id="two" class="map-marker">
    <span aria-hidden="true">2</span>
  </a>
</div>

它应该像

let pickerString2 = intArray.map
    {
        String($0)
    }

但是你试图用另一个为空的pickerString2变量填充pickerview。

pickerString2 = intArray.map
    {
        String($0)
    }