我在用字符串数组填充UIPickerView时遇到麻烦,每一行都是一个选择。我将在ViewController类中创建一个UIPicker插座,并根据main.async调用生成的数据对UIPicker进行更改。
// Creating our UIView Controller WeldFloorController
// WeldFloorController provides functionality to the WeldFloor screen
class WeldFloorController: UIViewController{
// Defining the regex String to be used
let regexString = String( """
((?:<div id=\\d{1,3}>)(UID:\\d{1,3})(currentPartNumber:(.{0,20}))(workcenter:(.{0,20}))(cycleTime:(.{0,20}))(curPartCycleTime:(.{0,20}))(partsMade:(.{0,20}))(CycleTimeActual:(.{0,20}))(target:(.{0,20}))(actual:(.{0,20}))(downtime:(.{0,20}))(statusReason:(.{0,30}))(lineStatus:(.{0,50}))(efficiency:(.{0,20}))(plusminus:(.{0,20}))(curProdTime:(.{0,30}))(<\\/div>))
""");
// Defining Main Thread for Data updates
let main = DispatchQueue.main;
// Intializing all usable objects on screen
@IBOutlet weak var weldFloorProductionPicker: UIPickerView!;
@IBOutlet weak var weldFloorDataDisplay: UICollectionView!;
// Configuring objects on screen happens within viewDidLoad()
// P*S Since the exectution is happening asynchronous so
// all updates must be made from within WorkcenterStatus.sharedInstance.run { }
override func viewDidLoad() {
// INITIAL CONFIGURATION OF OBJECTS (SUCH AS defaultText etc..) DONE HERE
// END CONFIG
// Starting WebService
WorkcenterStatus.sharedInstance.run { result in
switch result {
// If Successful then execute main.async
// In other words execute everything in .success
// when webService == Successful
// [execute main.async] { code here }
case .success(let htmlBody): self.main.async {
// First operation -- Return all matches into an array of Strings
let returnHtml = matches(for: self.regexString, in: htmlBody);
// Second operation -- For each match return an integer
let dataPoints = returnHtml.count;
// Third operation -- Populate our UICollections with our data
// Sub Operation #1 -- Creating the dataSource
// Edit** I kept this here for one thing ^^ DataSources should be called
// from a seperate class file or library -- Using dataSources built within
// the viewController are not reusable unless the dataSource is delegated
// to another ViewController which at the point it would be just better
// to create another lib file
// and call that from there
// Sub Operation #1 Actual -- Setting a dataSource object
self.weldFloorProductionPicker.dataSource = returnHtml[];
};
// If failed then print error
case .failure(let error): print(error)
};
};
// Additional Setup
super.viewDidLoad()
// Do any additional setup after loading the view.
};
};
self.weldFloorProductionPicker.dataSource = returnHtml[];
这不是识别我要将dataSource设置为数组及其所有值的有效方法吗?数组是一维的,我得到的错误是Cannot subscript a value of type '[String]' with an index of type '()'
任何帮助将不胜感激
谢谢
答案 0 :(得分:1)
您可以尝试
class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource {
var arr = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.weldFloorProductionPicker.delegate = self
self.weldFloorProductionPicker.dataSource = self
WorkcenterStatus.sharedInstance.run { result in
switch result {
case .success(let htmlBody):
let returnHtml = matches(for: self.regexString, in: htmlBody)
self.arr = returnHtml
DispatchQueue.main.async {
self.weldFloorProductionPicker.reloadAllComponents()
}
}
// If failed then print error
case .failure(let error):
print(error)
}
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arr[row]
}
}