swift - 按 double 对 UITableView 进行排序

标签 swift firebase firebase-realtime-database

我正在尝试创建一个 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”。非常感谢任何帮助,非常感谢。

最佳答案

你必须:

  1. 根据距离或您依赖的任何值对数据数组进行排序。
  2. 调用tableview的reloadData函数生效。

希望这有帮助

关于swift - 按 double 对 UITableView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49719828/

相关文章:

ios - 类型[字符串: String] does not conform to protocol 'AnyObject'

ios - 拥有同步线程的目的

java - 离线时如何丢弃挂起的 DocumentReference 写入?

swift - 如何在 firebase 存储引用观察者中进行计算?

firebase - 实现 Codelab Firebase 聊天示例

Firebase 实时数据库 - 路径和键的限制?

ios - 在 Swift 中使用 SPUserResizableView

swift - 多个 SKSpriteNodes 作为 Swift 中的一个节点

java - 如何将检索到的数据从 firebase 保存到 android 中的字符串变量?

java - Firebase 帮助类