我试图将现有的表视图指向新的DynamoDB数据库。 AWS DynamoDB调用在表视图中填充了一个字典变量数组,但模拟器正在显示数据。我花了几天的时间尝试使用异步函数调用来完成闭包,但没有成功。现在,我摆脱了自定义功能,直接在表视图的viewDidLoad()中使用AWS闭包。任何帮助表示赞赏。
这是表格视图代码:
override func viewDidLoad() {
super.viewDidLoad()
//dynamodb call
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
dynamoDBObjectMapper.scan(Employees.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
}
let paginatedOutput = task.result!
for emp in paginatedOutput.items as! [Employees] {
self.myVariables.empDict["empid"] = emp._empid
self.myVariables.empDict["email"] = emp._email
self.myVariables.empDict["firstname"] = emp._firstname
self.myVariables.empDict["lastname"] = emp._lastname
self.myVariables.empDict["location"] = emp._location
self.myVariables.empDict["mobile"] = emp._mobile
self.myVariables.empDict["work"] = emp._work
self.myVariables.empDict["site"] = emp._site
self.myVariables.arrayEmployees.append(self.myVariables.empDict)
//print(self.myVariables.arrayEmployees) // this works
} // for loop
self.employeeSearch = self.myVariables.arrayEmployees
print("printing employeeSearch")
print(self.employeeSearch) // This works
// self.employee1View.reloadData()
// tried reloading here (above - showing here as commented), but getting error: UITableView.reloadData() must be used from main thread only
return nil
} // dynamoDBObjectMapper.scan
// self.employee1View.reloadData()
// Then I tried reload above (showing here as commented), but I get error : Expected ',' separator. If I accept compiler's suggestion, it puts a comma just after curly braces above and that causes more errors at the Line dynamoDBObjectMapper.scan error : Cannot invoke 'continueWith' with an argument list of type '(block: (AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any?, Void)'
) // .continueWith
// end dynamodb call
// other things in this overload function
// ..
}
}
然后,当我运行代码时,我在覆盖表视图中添加了一条打印命令,但它显示的是[[]之类的空白数组。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("printing employeSearch from numberOfRowsInSection")
print(employeeSearch) // This returns blank like []
if isSearching {
return currentEmployeeSearch.count
}
return employeeSearch.count
}
我尝试过多次尝试重新加载表视图,但这也无济于事。
这是具有isSearching变量的搜索功能
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
employee1View.reloadData()
} else {
isSearching = true
employeeSearch = empListDict // getSwiftArrayFromPlist()
currentEmployeeSearch = employeeSearch.filter {($0["lastname"]?.lowercased().contains(searchText.lowercased())) ?? false}
employee1View.reloadData()
}
}
答案 0 :(得分:1)
ok尝试在您获取数据后(例如在封存中)重新加载数据。
您必须像这样在主线程中重新加载数据:
DispatchQueue.main.async {
self.employee1View.reloadData()
}