首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在从tableview中删除特定节后隐藏表视图中的各节?

如何在从tableview中删除特定节后隐藏表视图中的各节?
EN

Stack Overflow用户
提问于 2017-06-16 08:47:13
回答 5查看 134关注 0票数 0

我有三个部分(见图),其中一个部分显示项目,剩下的两个单元格现在使用闪存板设计,如果我使用第一节中的“删除”按钮删除所有项目,那么剩下的两个部分需要隐藏,并显示一些文本,任何人可以帮助我这样做吗?

代码语言:javascript
复制
func numberOfSections(in tableView: UITableView) -> Int{
    // #warning Incomplete implementation, return the number of sections
    return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if (section == 0){
        return itemsArray.count
    }else{
        return 1
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.section == 0{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! productTableViewCell
        tableDetails.isHidden = false
        myActivityIndicator.stopAnimating()
        let arr = itemsArray[indexPath.row]
        let urls = NSURL(string: arr["img"] as! String)
        let data = NSData (contentsOf: urls! as URL)
        cell.imageview.image = UIImage(data: data! as Data)
        cell.nameLabel.text = arr["productName"]as! String
        var price = arr["productPrice"] as! String
        print(price)
        var Quantity : Float = 1
        let itemId : Int =  arr["sku"] as! Int
        print(itemId)
        for aDic in CartArray{
            if aDic["id"] == String(itemId){

                Quantity = Float(String(aDic["quantity"]!))!
            }
        }
        print(CartArray)
        cell.stepper.value = Double(Int(Quantity))
        cell.stepper.tag = indexPath.row
        cell.stepper.addTarget(self, action: #selector(stepperValueChange(stepper:)), for:.valueChanged)
        price = price.replacingOccurrences(of: "KD", with: "")
        cartstring = String(Float(price)! * Quantity) + "0KD"
        cell.priceLabel.text = cartstring
        let quantityText = String(Quantity)
        let endIndex = quantityText.index(quantityText.endIndex, offsetBy: -2)
        let truncated = quantityText.substring(to: endIndex)
        cell.quantityTextField.text = truncated
        cell.price = price
        cell.deleteButton.addTarget(self, action: #selector(deleteButtonAction(button:)), for: .touchUpInside)
        cell.deleteButton.tag = indexPath.row
        return cell
    }else if indexPath.section == 1{
        let cell = tableView.dequeueReusableCell(withIdentifier: "couponcell", for: indexPath) as! CouponTableViewCell
        cell.applyButton.addTarget(self, action: #selector(applyButtonAction(button:)), for: .touchUpInside)
        return cell
    }else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "checkout", for: indexPath) as! checkoutTableViewCell
        cell.finalCartpriceLabel.text = total
        return cell
    }
}
func deleteButtonAction(button : UIButton) {
        let buttonPosition = button.convert(CGPoint(), to: tableDetails)
        let index = tableDetails.indexPathForRow(at: buttonPosition)
        self.itemsArray.remove(at: (index?.row)!)
        self.tableDetails.deleteRows(at: [index!], with: .automatic)
        tableDetails.reloadData()
     }
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2017-06-16 12:29:19

这段代码对我非常有用。

代码语言:javascript
复制
func deleteButtonAction(button : UIButton) {
        let buttonPosition = button.convert(CGPoint(), to: tableDetails)
        let index = tableDetails.indexPathForRow(at: buttonPosition)
        self.itemsArray.remove(at: (index?.row)!)
        self.tableDetails.deleteRows(at: [index!], with: .automatic)
        tableDetails.reloadData()
        if (tableView(tableDetails, numberOfRowsInSection: 0) == 0){
            tableDetails.isHidden = true
        }
        if (tableDetails.isHidden == true){
            self.loadingLabel.textColor = UIColor.gray
            self.loadingLabel.textAlignment = NSTextAlignment.center
            self.loadingLabel.text = "Your shopping cart is empty"
            self.loadingLabel.frame = CGRect(x: 130, y: 320, width: 140, height: 30)
            view.addSubview(loadingLabel)
            }
}
票数 0
EN

Stack Overflow用户

发布于 2017-06-16 09:02:11

您只需检查您的self.itemsArray.count > 0中的func numberOfSections(in:),并显示此条件的所有三个部分。否则,只返回第一个部分,这将自动隐藏另外两个部分。

示例:

代码语言:javascript
复制
func numberOfSections(in tableView: UITableView) -> Int {
    if self.itemsArray.count > 0 {
        return 3
    }
    return 1
}
票数 0
EN

Stack Overflow用户

发布于 2017-06-16 09:05:20

使用以下内容修改numberOfSections

代码语言:javascript
复制
func numberOfSections(in tableView: UITableView) -> Int {
    if self.itemsArray.count > 0 {
        return 3
    }
    //Show Message List is Empty
    return 1
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44584785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档