【问题标题】:How can i sort my tableView data with Segment Controller?如何使用段控制器对我的 tableView 数据进行排序?
【发布时间】:2018-03-28 14:43:26
【问题描述】:

[在顶部注意到一个段控制器,我想处理 tableView 所有数据将在段调用时排序`enter

我想在 segmentController 的帮助下处理我的模型产品数据

//数据库中的产品

    DataService.ds.REF_Products.observe(.value, with: { (snapshot) in

        if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
            for snap in snapshot {
                print("SNAP: \(snap)")
                if let productDict = snap.value as? Dictionary<String, AnyObject> {
                    //                        let productName = productDict["productName"]
                    let key = snap.key
                    let product = AddProductModel(productKey: key, productData: productDict)
                    self.products.insert(product, at: 0)

                    //                        self.produtsArray.append(productName as! String)
                    //                        print(self.produtsArray)
                }

            }
            self.productTable.reloadData()
        }


    })


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let product = products[indexPath.row]

    if let cell = productTable.dequeueReusableCell(withIdentifier: "PlistCell") as? ProductListTableCell {

        if let img = EmployeeHomeVC.imageCache.object(forKey: product.productimageUrl as NSString) {

            cell.configureCell(product: product, img: img)

        } else {
            cell.configureCell(product: product, img: nil)
        }

        let bgColorView = UIView()
        bgColorView.backgroundColor = UIColor.brown
            cell.selectedBackgroundView = bgColorView


        return cell
    } else {
        return ProductListTableCell()
    }
}

这里`]1

【问题讨论】:

    标签: uitableview firebase swift3 xcode8 ios10


    【解决方案1】:

    每次单击分段选项时,在分段按钮的操作中,您可以调用排序方法,也可以使用 Swift 提供的默认排序:

    products.sort(by: {$0.criteriaOfSort &gt; $0.criteriaOfSort })

    排序后你打电话 productTable.reloadData()

    一切都会按照你的意愿排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-13
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多