gpt4 book ai didi

ios - 当 UITableView 为空时隐藏 UIButtons

转载 作者:行者123 更新时间:2023-11-28 15:28:54 24 4
gpt4 key购买 nike

  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()

}

}

最佳答案

根据 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
}

滑动删除单元格时:

在从购物车 TableView 中删除单元格时,您必须通过检查 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")

}

关于ios - 当 UITableView 为空时隐藏 UIButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44932222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com