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

问题描述:

我有我的表格视图中显示的产品项目列表。同时,我还有其他api电话,我会通过我的产品名称来检查。如果该产品项目可用,那么该特定数据或项目单元格将被突出显示,并且将被禁用。如何从表格视图中删除特定数据

现在我需要的是,当我做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中cell.How数据要做到这一点?

感谢

+0

显示您模型you're使用填充你的tableView。 –

+0

@RashwanL我已更新。就像我如何将数据填充到我的表格视图 – david

+0

@RashwanL这个'var filtered:[allitems]?'allitems'包含产品名称tahst,就像'var name =“”'一样,它的地图像'func mapping (地图:地图){ vpaname david

正如你所描述的,如果你的数据是这样的:

name1, name2,name3, name4 

然后,您想要在tableView中显示四行。

如果name2是在你的API调用可用的,那么你要显示的:

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() 
} 
+0

暗示我得到这个错误类型'allitems'不符合协议'序列'在国内生产线上的序列'''产品{' – david

+0

@david,把你的产品模型添加到你的答案中。或者甚至将您的项目上传到Dropbox或Google Drive,然后在此处发布链接,我将查看您的项目。 –

+0

我会尝试上传我的项目。你可以请看看这个。即使这样我也无法解决https://*.com/questions/46641892/table-view-check-box-not-accepting-action-for-index-0 – david

首先在cellForRow不会做这种检查,使其在viewDidLoadviewWillAppear

  • 属性var isAvailable = false到您的产品型号
  • 添加在viewDidLoadviewWillAppear检查产品的可用性并相应地设置isAvailable
  • 创建数据源阵列var filtered = [Product]()(假设Product的数据模型)和过滤项目filtered = allItems.filter { $0.isAvailable }
  • 刷新表