如何从表视图中删除特定数据

时间:2017-10-21 04:07:07

标签: ios swift uitableview

我有要在我的表格视图中显示的产品项目列表。同时我还有一些其他api电话,我将通过我的prodcut项目名称进行检查。如果该产品项目可用,则仅突出显示该特定数据或项目单元格,并且将禁用该项目。

现在我需要的是,当我进行api调用时,之后如果该api中的特定数据或产品名称可用,而不是突出显示和禁用...我不应该在我的表视图中显示该特定数据

如何做到这一点:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AllConnectionsTableViewCell

 if let contact = filtered?[indexPath.row]{
                cell.emailOutlet.text = AccountDataCache.sharedInstance.displayMaskAccnt(items: product.name)
                cell.nameOutlet.text = product.name
    if let _ = self.checkapicall(items: product.name){

                      // here if my product name is availble in api, then only the backgroudnd and it will be disabled



if let product = filtered?[indexPath.row]{

                cell.namelabel.text = product.name
 if let _ = self.checkapicall(items: product.vpa){



                    cell.cellOuterView.backgroundColor = UIColor.red
                    cell.isUserInteractionEnabled = false

                }else{
                    cell.cellOuterView.backgroundColor = UIColor.white
                    cell.isUserInteractionEnabled = true

                }

}


}

而不是chnaging BG,Disable..i不应该在该tableview单元格中显示该数据。如何做到这一点。?

由于

2 个答案:

答案 0 :(得分:1)

正如您所描述的,如果您的数据如下所示:

name1, name2,name3, name4

然后,您希望在tableView中显示四行。

如果您的API调用中有name2,那么您想要显示:

name1, name3, name4

因此,您需要做的是在开始更新tableView之前获取所有名称。这是因为您需要设置要在tableView中显示的行数。

你可以这样做(我不确定你今天如何获取你的数据,但这是一个让你入门的例子):

// check add edit to your product
var products = [Product(name: "name1", vpa: "1"), Product(name: "name2", vpa: "2"), Product(name: "name3", vpa: "3"), Product(name: "name4", vpa: "4")]

// set the produts count
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return products.count
}

// just set the name here, don´t make any checks
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "StartPageCell", for: indexPath)
    cell.namelabel.text = product.name
    return cell
}

// check the names here and then reload the tableView
func checkNames() {
    for product in products {
        if self.checkapicall(items: product.vpa){ {
             if let index = products.index(where: { $0.vpa == vpa }) {
            products.remove(at: index)
        }
        }
    }
    tableView.reloadData()
}

答案 1 :(得分:0)

首先,请勿在{{1​​}}中进行此类检查,并将其设为cellForRowviewDidLoad

  • 将属性viewWillAppear添加到您的商品型号
  • var isAvailable = falseviewDidLoad中检查产品的可用性并相应地设置viewWillAppear
  • 创建数据源数组isAvailable(假设var filtered = [Product]()是数据模型)并过滤项Product
  • 重新加载表格