如何从表格视图中删除特定数据
问题描述:
我有我的表格视图中显示的产品项目列表。同时,我还有其他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数据要做到这一点?
感谢
答
正如你所描述的,如果你的数据是这样的:
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()
}
答
首先在cellForRow
不会做这种检查,使其在viewDidLoad
或viewWillAppear
- 属性
var isAvailable = false
到您的产品型号 - 添加在
viewDidLoad
或viewWillAppear
检查产品的可用性并相应地设置isAvailable
。 - 创建数据源阵列
var filtered = [Product]()
(假设Product
的数据模型)和过滤项目filtered = allItems.filter { $0.isAvailable }
- 刷新表
显示您模型you're使用填充你的tableView。 –
@RashwanL我已更新。就像我如何将数据填充到我的表格视图 – david
@RashwanL这个'var filtered:[allitems]?'allitems'包含产品名称tahst,就像'var name =“”'一样,它的地图像'func mapping (地图:地图){ vpaname david