gpt4 book ai didi

ios - 字符串数组作为 UIPickerView 的数据源

转载 作者:行者123 更新时间:2023-11-28 13:32:47 24 4
gpt4 key购买 nike

我在使用字符串数组填充 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[];这不是确定我想将数据源设置为数组及其所有值的有效方法吗?数组是一维的,我得到的错误是

谢谢,

最佳答案

你可以试试

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

}

关于ios - 字符串数组作为 UIPickerView 的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57097705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com