我正在尝试创建一个 UITableView,它根据与用户的距离对商店进行排序。虽然我的距离计算器可以工作,但我不知道如何按这个值(即 h)对 UITableView 进行排序。这是我当前的代码:
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var nearbyTableView: UITableView!
var myList: [String] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]
cell.textLabel?.text = cellName
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var cellName = myList[indexPath.row]
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
viewController.name = cellName
self.present(viewController, animated: true, completion: nil)
navigationController?.pushViewController(viewController, animated: true)
print("row\(indexPath.row)")
print("name: \(cellName)")
}
if 语句计算商店的距离。但是,我尝试了很多不同的方法,但我不知道如何用这个值对“myList”进行排序。它说“myList 没有成员 h”。非常感谢任何帮助,非常感谢。
最佳答案
你必须:
- 根据距离或您依赖的任何值对数据数组进行排序。
- 调用tableview的reloadData函数生效。
希望这有帮助
关于swift - 按 double 对 UITableView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49719828/