我正在尝试使用API自动填充文本字段(用于全局股票自动填充)。 当用户点击文本字段并开始编辑时,将显示包含响应中所有数组值的表视图。当用户第一次编辑文本字段时,我能够填充表视图,但稍后表视图数据不会被更改。 但我尝试打印我的数组,并且数组中的值根据UITextField中输入的字符串而变化。
var stockValues = [String]()
var getStockName:String = ""
override func viewDidLoad() {
super.viewDidLoad()
//Intialising myCustome Table View AutocompleteStocks
AutocCompleteStocks.delegate = self
AutocCompleteStocks.dataSource = self
self.AutocCompleteStocks.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
self.AutocCompleteStocks.isHidden = true
}
@IBOutlet var stocksAutocomplete: UITextField!
@IBOutlet var AutocCompleteStocks: UITableView!
//TableView Delegate Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("TejastockValues\(self.stockValues.count)")
return self.stockValues.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell")
cell?.textLabel?.text = self.stockValues[indexPath.row]
print("TejastockValues\(self.stockValues)")
return cell!
}
@IBAction func buttonPressed(_ sender: AnyObject) {
}
@IBOutlet var stockEntryTextField: UITextField!
//Text Field when editing changes . I am storing the text value entered by user into getStockName variable
@IBAction func AutoCompleteTextField(_ sender: AnyObject) {
self.AutocCompleteStocks.isHidden = false
getStockName = stockEntryTextField.text!
print("kichuuuuu\(getStockName)")
//Calling web service
Alamofire.request("http://search.xignite.com/Search/Suggest?parameter=XigniteGlobalQuotes.GetGlobalDelayedQuote.Identifier&term=\(getStockName)&tags=xnse").responseJSON{response in
if let json = response.result.value as? [String:AnyObject]{
if let results = json["Results"]{
print(" \(results)")
//looping through all the values
for i in 0..<results.count{
let item = results[i] as! [String:AnyObject]
let value = item["Value"] as! String
self.stockValues.append(value)
self.AutocCompleteStocks.reloadData()
//reloading the tableview after appending values
}
print("TejastockValues\(self.stockValues)")
}
}
}
callingAutocomplete()
}
func callingAutocomplete(){
}
答案 0 :(得分:1)
您不应该从后台线程重新加载tableView
,始终在main
线程上。
此外,由于您在向tableView
附加值时正在重新加载stockValue
,因此您可以重新加载tableView
次,与您拥有的值一样多。但是,只有拥有所有项目后才能进行。
在for
循环中执行以下操作:
for i in 0..<results.count {
let item = results[i] as! [String:AnyObject]
let value = item["Value"] as! String
self.stockValues.append(value)
}
DispatchQueue.main.async { [weak self] in
//Reloading the tableview after ALL values appended.
self?.AutocCompleteStocks.reloadData()
}
答案 1 :(得分:0)
当UIText字段发生更改时,您正在将值附加到stockValues而不清除它。做点什么
在TextField事件更改中@IBAction func AutoCompleteTextField(_ sender: AnyObject) {
self.AutocCompleteStocks.isHidden = false
getStockName = stockEntryTextField.text!
print("kichuuuuu\(getStockName)")
//Calling web service
Alamofire.request("http://search.xignite.com/Search/Suggest?parameter=XigniteGlobalQuotes.GetGlobalDelayedQuote.Identifier&term=\(getStockName)&tags=xnse").responseJSON{response in
if let json = response.result.value as? [String:AnyObject]{
if let results = json["Results"]{
print(" \(results)")
//looping through all the values
for i in 0..<results.count{
let item = results[i] as! [String:AnyObject]
let value = item["Value"] as! String
let text = item["Text"] as! String
self.stockValues.append(text)
//reloading the tableview after appending values
}
self.AutocCompleteStocks.reloadData()
print("TejastockValues\(self.stockValues)")
}
}
}
DispatchQueue.main.async{
self.AutocCompleteStocks.reloadData()
}
stockValues = [""]
}