如何在 swift 中为 UIPickerview 添加新元素到 2 组件数组
how to add a new element to a 2 component array for UIPickerview in swift
当我尝试添加带有 .count 的 newItem 时,没有任何显示。当我使用它替换 "car" 时,我在 pickerview 中看不到它,而且在 newItem 出现之前我需要上下滚动几次。
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)
}
我认为人们不会以这种方式做您想做的事。通常,我会创建一个 UITableView
和自定义 UITableViewCell
来实现动态列表。 UIPickerView
是完全预定义的,没有那么多动态视图。
虽然对于您的代码,如果您真的非常需要 UIPickerView
是动态的,我会说
Add self.pickerView.setNeedsLayout()
after you change your datasource, pickerViewArray
for your case.
我在 XCode9 beta3 上测试过,目标是 iOS 8.0,Swift 3.
当我尝试添加带有 .count 的 newItem 时,没有任何显示。当我使用它替换 "car" 时,我在 pickerview 中看不到它,而且在 newItem 出现之前我需要上下滚动几次。
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)
}
我认为人们不会以这种方式做您想做的事。通常,我会创建一个 UITableView
和自定义 UITableViewCell
来实现动态列表。 UIPickerView
是完全预定义的,没有那么多动态视图。
虽然对于您的代码,如果您真的非常需要 UIPickerView
是动态的,我会说
Add
self.pickerView.setNeedsLayout()
after you change your datasource,pickerViewArray
for your case.
我在 XCode9 beta3 上测试过,目标是 iOS 8.0,Swift 3.