首页 > 解决方案 > 如何过滤我的表格视图的内容?

问题描述

我有一个 tableView 显示用户的电子邮件和出生数据,问题是当前用户的电子邮件和出生日期也显示在列表中。我该如何避免呢?

这是我尝试过的代码,电子邮件来自 emailList[indexPath.row]。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "emailCell", for: indexPath)

    let snapshot = emailList[indexPath.row]

    if let userDictionary = snapshot.value as? [String:AnyObject] {
        if let email = userDictionary["email"] as? String {
        if let DOB = userDictionary["DOB"] as? String {

        cell.textLabel?.text = email
        cell.detailTextLabel?.text = DOB

          }
      }
  }

我想在 tableView 中查看用户电子邮件和出生日期的列表,但没有当前用户信息。

标签: swifttableview

解决方案


获得当前用户数据以及 emailList 后,您可以使用类似这样的方式过滤 emailList

func downloadData(_ completionHandler: (AnyObject?) -> ()){
 let data:AnyObject? = //perform the logic to download data
 completionHandler(data)
}

override func viewDidLoad() {
    super.viewDidLoad()
    downloadData(){ data in
      //Do stuff with data - parse it etc.
       guard let emailList = emailList as? [[String:String]] // cast to the type you need
       else { 
         return  // Do some error handling
       } 
       let filteredData = emailList.filter { (dict: [String:String]) -> Bool in
         if dict["email"] == currentEmail && dict["user"] == currentUsername {
           return false
         }
         return true
       }
      self.tableView.delegate = self
      self.tableView.dataSource = self
    }
}

推荐阅读