Ios UITableView为空时隐藏UIButtons
当tableView为空时,上面的代码将隐藏对象。但是,当我在其中添加项目时(它应该是一个购物车),Ios UITableView为空时隐藏UIButtons,ios,swift,uitableview,swift3,uibutton,Ios,Swift,Uitableview,Swift3,Uibutton,当tableView为空时,上面的代码将隐藏对象。但是,当我在其中添加项目时(它应该是一个购物车),totalproceedbtn和totalab仍然隐藏 有没有办法重新加载表并检查它是否为空 注: 我的单元格可以滑动,我可以通过滑动删除单元格 func numberOfSections(in tableView: UITableView) -> Int { if carts.count > 0 { self.tableView.backg
total
proceedbtn
和totalab
仍然隐藏
有没有办法重新加载表并检查它是否为空
注:
我的单元格可以滑动,我可以通过滑动删除单元格
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
}
更新
获取数据:
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()
}
}
您可以在您有
print(“delete”)
的地方调用,也可以从添加新车的任何地方调用。基于cards
数组计数启用/禁用按钮
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
}
通过滑动删除单元格时:
在从购物车表视图中删除单元格时,必须通过检查购物车的计数来禁用该按钮
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
是否为空来隐藏它们。方便的是,苹果公司提供了.isEmpty
功能为您提供这些信息。这很有效!谢谢当我访问视图时,购物车已经空了,怎么样?我尝试在viewDidLoad中实现if cart.isEmpty
,但在打开视图时无法运行。请检查carts.isEmpty
。并确保您的数组已分配干燥,但当您将项目添加到卡中时,它会隐藏按钮和标签,即使购物车没有空?您在哪里分配卡
阵列?让我们看看。您能显示/告诉我们您用于在其中添加项目的代码吗?请检查更新的问题
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
}
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")
}