从 UITableView 隐藏单元格

Hide cell from UITableView

我正在尝试隐藏 UITableView 中的单元格。我的代码如下。

当我打开应用程序时,我在 TableView 中看到空行as you can see here

如何从 UITableView 中隐藏或移除(而不是删除)空单元格?

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedTableViewCell
    
    let row = self.items[indexPath.row]
    
    cell.lblTitle.text = row.title
    cell.isHidden = !checkCurrentUser(email: row.email)
   
    return cell
}

我添加了过滤数组,但随后出现了不同的错误 like this。我的新代码如下。我该如何解决这个问题?

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedTableViewCell
    
    
    let row = self.items[indexPath.row]
    self.items = self.items.filter{checkCurrentUser(email: [=11=].email)}
    cell.lblTitle.text = row.title
    //cell.isHidden = !checkCurrentUser(email: row.email)
    
    
    
    return cell
}

全部代码如下

import UIKit
import Firebase

class OyuncularVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    
    var items = [ItemModel]()
    
    @IBOutlet weak var tblView: UITableView!
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
       
        tblView.tableFooterView = UITableViewHeaderFooterView()
        retrieveItems()
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedTableViewCell
        
        
        let row = self.items[indexPath.row]
        self.items = self.items.filter{checkCurrentUser(email: [=12=].email)} //bunu ekledim siliceksem bunu silicem aga
        cell.lblTitle.text = row.title
        //cell.isHidden = !checkCurrentUser(email: row.email)
        
        
        
        return cell
    }
 
    
   
    
    /* Retriev Items */
    
    func retrieveItems() {
        DataService.dataService.ITEM_REF.observe(.value, with: { (snapshot: DataSnapshot?) in
            
            if let snapshots = snapshot?.children.allObjects as? [DataSnapshot] {
                self.items.removeAll()
                print(snapshots.count)
                for snap in snapshots {
                    if let postDic = snap.value as? Dictionary<String, AnyObject> {
                        
                        let itemModel = ItemModel(key: snap.key, dictionary: postDic)

                        print(itemModel)
                        self.items.insert(itemModel, at: 0)
                    }
                }
                
                self.tblView.reloadData()
            }
            
        })
        
    }
    
    func checkCurrentUser(email: String) -> Bool {
        let currentUser = Auth.auth().currentUser
        
        return email == currentUser?.email
    }
  


    }
    

}

如果您只想显示当前用户的电子邮件,您可以过滤数据库中的项目(应用谓词),这是最有效的方法。

或过滤 for snap in snapshots 循环中的项目。

但是,如果您想保留整个数据集,请声明第二个数组

var items = [ItemModel]()
var filteredItems = [ItemModel]()

并替换

for snap in snapshots {
    if let postDic = snap.value as? Dictionary<String, AnyObject> {
        
        let itemModel = ItemModel(key: snap.key, dictionary: postDic)

        print(itemModel)
        self.items.insert(itemModel, at: 0)
    }
}

通过以下它在循环中执行检查

let currentUser = Auth.auth().currentUser
self.filteredItems.removeAll()
for snap in snapshots {
    if let postDic = snap.value as? Dictionary<String, AnyObject> {
        
        let itemModel = ItemModel(key: snap.key, dictionary: postDic)

        print(itemModel)
        self.items.insert(itemModel, at: 0)
        if itemModel.email == currentUser?.email {
           self.filteredItems.insert(itemModel, at: 0)
        }
    }
}

并将两个数据源方法替换为

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedTableViewCell
    
    let row = self.filteredItems[indexPath.row]
    cell.lblTitle.text = row.title  
    return cell
}

并删除方法checkCurrentUser