按状态对表格视图数据进行排序的最佳方法,我提供了以下代码。所有状态"好"处于最佳状态"糟糕"在下面或将跟随。等于良好的状态必须是等于坏状态的最高状态。此外,我认为最好在获得响应之前对其进行排序,然后再将其附加到getalldetail
case .success(_):
guard let json = response.result.value as! [[String:Any]]? else{ return}
print("Api Response : \(json)")
let filtered = json.filter { (dict) -> Bool in
guard let status : [String : String] = dict["status"] as? [String : String], let statusString = status["name"] else {
return false
}
var a = statusString.compare("test") != .orderedSame
var b = statusString.compare("testing") != .orderedSame
return a && b
}
for item in json {
self.getAllDetail = filtered
self.tableView.reloadData()
self.refresher.endRefreshing()
self.activityIndicator.stopAnimating()
// UIApplication.shared.beginIgnoringInteractionEvents()
}
if !self.getAllDetail.isEmpty{
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
break
case .failure(_):
print("Error")
break
}
}
var getAllDetail: [[String:Any]] = [[String:Any]]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return getAllDetail.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "ToDoListTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ToDoListTableViewCell
cell.delegate = self
// let toDoActionItem = fetchedResultsController.object(at: indexPath)
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
if let str = getTempDetails["status"] as? [String: String] {
if let name = str["name"] {
if name == "good" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = created
// cell.label.textColor = UIColor(red: 0.9294, green: 0.3333, blue: 0.1804, alpha: 1.0)
// cell.backgroundColor = created
}else if name == "bad" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = done
cell.checkBoxButton.isSelected = true
}
} else {
print("false")
cell.toDoItemLabel.text = "Data"
}
}
}
}
答案 0 :(得分:0)
您的代码中有更多错误的内容。
您仅在成功阻止中移除活动指示器?在json中有项目循环吗?我可以知道你为什么这样做吗? :
let filtered = json.filter { (dict) -> Bool in
guard let status : [String : String] = dict["status"] as? [String : String], let statusString = status["name"] else {
return false
}
var a = statusString.compare("test") != .orderedSame
var b = statusString.compare("testing") != .orderedSame
return a && b
}
for item in json {
self.getAllDetail = filtered
self.tableView.reloadData()
self.refresher.endRefreshing()
self.activityIndicator.stopAnimating()
}
if !self.getAllDetail.isEmpty{
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
您可以通过以下方式替换此代码:
self.refresher.endRefreshing()
self.activityIndicator.stopAnimating()
//Remove activity indiactor when you get any response not just success, otherwise for failure it will keep aimating, then in success case
let filtered = json.filter { (dict) -> Bool in
guard let status : [String : String] = dict["status"] as? [String : String], let statusString = status["name"] else {
return false
}
var a = statusString.compare("test") != .orderedSame
var b = statusString.compare("testing") != .orderedSame
return a && b
}
if !self.getAllDetail.isEmpty{
DispatchQueue.main.async {
// Sorting using string comparison, g > b in ASCII value
self.getAllDetail = filtered.sorted { (firstObject, secondObject) -> Bool in
guard let statusDictFromFirst = firstObject["status"] as? [String: String], let statusKeyFromFirst = statusDictFromFirst["name"] , let statusDictFromSecond = secondObject["status"] as? [String: String], let statusKeyFromSecond = statusDictFromSecond["name"] else {
return false
}
return statusKeyFromFirst > statusKeyFromSecond
}
print("getAllData : \(getAllData)")
self.tableView.reloadData()
}
}
并在tableView
中显示没有任何if else
条件的数据,无论好坏。现在,所有good status
数据将会出现,之后bad status
将会出现。
还要了解completion callbacks
。它有助于async operations
获得API
响应并在获得响应后更新用户界面。