swift - 如何过滤我的表格视图的内容?
问题描述
我有一个 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 中查看用户电子邮件和出生日期的列表,但没有当前用户信息。
解决方案
获得当前用户数据以及 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
}
}
推荐阅读
- typescript - 具有枚举值的 TypeGraphql InputType 字段不允许输入字符串(引用文本)
- python - DevStack 放置-api 没有启动
- java - 如何将多个段落包装在一个元素中?
- r - 地址正确时如何修复错误的邮政编码
- java - 在 android 启动器中,如何阻止对某些应用程序的访问?
- c++ - 固定分配 std::vector
- python - 尝试仅使用 [HH:DD] 转换 Pandas 列,但返回 [YYYY-DD-MM HH:MM:SS]
- security - 如何在 Yesod 应用程序中向所有端点添加标头?
- mulesoft - 在 Mulesoft 中关闭注销
- python - 如何使用不同的列用 geopandas 绘制不同的颜色属性(色调、值、饱和度)?