【问题标题】:hide UIButtons when UITableView is empty当 UITableView 为空时隐藏 UIButtons
【发布时间】:2017-12-09 11:25:40
【问题描述】:
  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()

    }

}

【问题讨论】:

  • 你能告诉我们你用来添加项目的代码吗?
  • 请检查更新的问题

标签: ios swift uitableview swift3 uibutton


【解决方案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")

}

【讨论】:

  • 行得通!谢谢。当我访问视图并且购物车已经为空时怎么样?我尝试在 viewDidLoad 中实现if cart.isEmpty,但没有成功
  • 当您打开视图时,请检查carts.isEmpty。并确保您的数组已被分配
  • 试过了,但即使购物车不是空的,它也会隐藏按钮和标签
  • 您何时将项目添加到卡中?你在哪里分配cards 数组?
【解决方案2】:

听起来你想要类似的东西

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") 的地方调用它,也可以从添加新购物车的任何地方调用它。

【讨论】:

  • 购物车为空且所有 UI 均未隐藏
  • 对不起,我不确定我是否关注。我描述的逻辑使用了你的 tableView 的内容应该基于一些全局源的事实。在您的情况下,来源是carts,对吗?同样,您希望按钮的外观基于相同的来源。与其让按钮检查表格中的单元格数量,不如根据carts 是否为空来隐藏它们。方便的是,Apple 提供了 .isEmpty 函数来为您提供该信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多