当UITableView为空时隐藏UIButtons

时间:2017-07-05 17:09:48

标签: ios swift uitableview swift3 uibutton

  func numberOfSections(in tableView: UITableView) -> Int {
        if carts.count > 0 {
            self.tableView.backgroundView = nil
            self.tableView.separatorStyle = .singleLine
            return 1


        }

        let rect = CGRect(x: 0,
                          y: 0,
                          width: self.tableView.bounds.size.width,
                          height: self.tableView.bounds.size.height)
        let noDataLabel: UILabel = UILabel(frame: rect)

        noDataLabel.text = "nothing, the table is empty"
        noDataLabel.textColor = UIColor.white
        noDataLabel.textAlignment = NSTextAlignment.center
        self.tableView.backgroundView = noDataLabel
        self.tableView.separatorStyle = .none
      self.total.isHidden = true
       self.proceedbtn.isHidden = true
        self.totalLab.isHidden = true


        return 1
    }

当tableView为空时,上面的代码将隐藏对象。但是,当我在其中添加商品(它应该是购物车)时,total proceedbtntotalLab仍然隐藏!!

有没有办法重新加载表格并检查它是否为空?

注意:

我的细胞是可滑动的,我可以通过滑动删除细胞。

func deleteMail(_ path:IndexPath) {
    guard carts.count > path.row else {
        print("Index out of range")
        return
    }

    let entry = carts[path.row]
    deletItem(id: entry.meal_id)// call api to delete items
    carts.remove(at: (path as NSIndexPath).row);
    tableView.deleteRows(at: [path], with: .left);
    print("delete")

}

更新

override func viewDidLoad() {
    super.viewDidLoad()

    if carts.isEmpty == true {

        proceedbtn.isHidden = true
    } else {
        if carts.isEmpty == false {
            if proceedbtn.isHidden == true {


            proceedbtn.isHidden = false
        }
    }
    }

获取数据:

func getData () {


    Alamofire.request(url!, method: .post, parameters: param,encoding: URLEncoding.default, headers: headers).responseJSON { response in
        if let value: AnyObject = response.result.value as AnyObject? {
       // get data 

                 self.price = subJson["price"].stringValue
                 self.qty = subJson["qty"].stringValue
                 let id = subJson["id"].stringValue

              self.id = id
              self.name = subJson["meal"]["name"].stringValue


                let info = Cart(meal_id: self.id, qty: self.qty, meal_name: self.name, price: self.price)

                self.carts.append(info)
          }

        }
        self.tableView.reloadData()

    }

}

2 个答案:

答案 0 :(得分:1)

根据cards阵列计数启用/禁用按钮。

func numberOfSections(in tableView: UITableView) -> Int {

       self.total.isHidden = carts.isEmpty
       self.proceedbtn.isHidden = carts.isEmpty
       self.totalLab.isHidden = carts.isEmpty

        if carts.count > 0 {
            self.tableView.backgroundView = nil
            self.tableView.separatorStyle = .singleLine
            return 1


        }

        let rect = CGRect(x: 0,
                          y: 0,
                          width: self.tableView.bounds.size.width,
                          height: self.tableView.bounds.size.height)
        let noDataLabel: UILabel = UILabel(frame: rect)

        noDataLabel.text = "nothing, the table is empty"
        noDataLabel.textColor = UIColor.white
        noDataLabel.textAlignment = NSTextAlignment.center
        self.tableView.backgroundView = noDataLabel
        self.tableView.separatorStyle = .none

        return 1
    }

通过滑动删除单元格:

通过检查carts数组

的数量,您必须在从购物车表视图中删除单元格时禁用该按钮
func deleteMail(_ path:IndexPath) {
    guard carts.count > path.row else {
        print("Index out of range")
        return
    }

    let entry = carts[path.row]
    deletItem(id: entry.meal_id)// call api to delete items
    carts.remove(at: (path as NSIndexPath).row);

      //You have to disable the button while delete the cells from cart table view
      if (carts.isEmpty) {
         self.total.isHidden = true
         self.proceedbtn.isHidden = true
         self.totalLab.isHidden = true
      }

    tableView.deleteRows(at: [path], with: .left);
    print("delete")

}

答案 1 :(得分:0)

听起来像是想要像

这样的东西
func cartsDidUpdate() {
    let cartIsEmpty = carts.isEmpty
    total.isHidden = cartIsEmpty
    proceedbtn.isHidden = cartIsEmpty
    totalLab.isHidden = cartIsEmpty
    // Also any other UI-style logic based on cart being empty
}

您可以在print("delete")的哪个地方拨打电话,也可以从添加新购物车的地方拨打电话。