当我尝试添加新项时。不显示任何计数。当我使用它代替“汽车”时,我在pickerview中看不到它,而且我需要上下滚动几次,然后新项目才会出现。

var picker1 = String()
var picker2 = String()

var pickerViewArray = [["Items", "car"], ["Action", "Turn", "Hit", "Pull", "Scrape"]]

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

    return pickerViewArray.count
}

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    picker1 = pickerViewArray[0] [pickerView.selectedRow(inComponent: 0)] + ""
    picker2 = pickerViewArray[1] [pickerView.selectedRow(inComponent: 1)] + ""
}

func addItemsToArray () {
    stringWall = "Wall"
    addItemAtIndex = pickerViewArray[0].count
    pickerViewArray[0].insert(newItem, at: addItemAtIndex)
    allRoomArray[arraySelector][wallButton] = stringWall

    pickingRooms()
    print(newItem)
}

最佳答案

我不认为ppl这样做你想做的。通常,我会用定制的UITableView创建一个UITableViewCell来实现动态列表。UIPickerView是预先定义好的,而不是动态视图。
而对于您的代码,如果您真的非常需要将UIPickerView动态化,我会说
更改数据源后添加self.pickerView.setNeedsLayout(),对于您的案例添加pickerViewArray
我在XCode9 beta3上测试了它,在iOS 8.0上测试了target,使用了Swift 3。

关于arrays - 如何快速为UIPickerview的2组件数组添加新元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45662453/

10-15 08:08