ios - 使用过滤器使单元格自行删除

标签 ios swift database firebase alamofire

对编码相当陌生!

我正在尝试使用 firebase 创建一个用户数据库,我可以在应用程序中过滤该数据库。我认为最简单的方法是在另一个 View Controller 上切换过滤器,然后使用 if 语句让不满足过滤条件的单元格自行删除。我希望总共有大约 6 个左右的过滤器,并且我认为这种方法是可扩展的。

我不知道如何让单元格自行删除。

此页面上只有一个过滤器。先前的 View Controller 有一个按钮,可以在 1 和 0 之间切换“maleFilter”。当男性过滤器为 1 时,我希望性别不为“Male”的用户删除自己。我怎样才能做到这一点?

谢谢!

///设置并从 Firebase 获取数据库

import UIKit
import Alamofire
import Gloss

class UserTableViewController: UITableViewController {

var user: User?
var users: [User] = []
var myIndex = 0
var maleFilter: Int?


override func viewDidLoad() {
    super.viewDidLoad()
    Alamofire.request("https://database.firebaseio.com/users.json").responseJSON(completionHandler: {
        response in

        if let usersDictionary = response.result.value as? JSON {

            for (key, value) in usersDictionary {

                if let usersDictionary = value as? JSON {
                    var user = User(json: usersDictionary)
                    user?.userKey = key
                    self.users.append(user!)
                    self.tableView.reloadData()
                }
            }
        }
    })
}

///这就是我苦苦挣扎的地方
覆盖 func viewWillAppear(_animated: Bool) { super.viewWillAppear(true)

//maleFilter has its value toggled between 1 and 0 in a prior view controller
print(maleFilter)

//maleFilter == 1 represents the filter being toggled on
if maleFilter == 1 {
        if user?.userGender == "Male" {
            print ("male")
        } else {

我已经尝试过在网上找到的这段代码,但无法让它工作

            /*
             users.remove(at: indexPath)
             users.removeAtIndex(deletionIndexPath.row)
             tableView.deleteRows(at: [indexPath.row], with: UITableViewRowAnimation)
             */

我也在网上找到了这个,但无法让它工作

      /*
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (rowAction: UITableViewRowAction, indexPath: IndexPath) -> Void in
            func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

                self.tableView.deleteRows(at: [indexPath], with: .automatic)
                self.tableView.reloadData()
            }
        }
   */
 } else {
        print("Male filter not applied")
    }
    self.tableView.reloadData()
}

我觉得答案可能非常简单。类似于“self.users.remove(at: indexPath) (这不起作用”

非常感谢任何帮助

最佳答案

    filterArray = UsersArray.filter({ m in
m.UserGender.contains("Male")
})

试试这个,结果你只得到男性的过滤器数组。 希望对您有帮助。

关于ios - 使用过滤器使单元格自行删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45372723/

相关文章:

ios - PushKit PKPushRegistryDelegate 没有得到回调

xcode - Swift 拖放在 Xcode 之外不起作用

mysql - 仅计算两个不同列中的空值并显示在一个选择语句中

arrays - 空数组可以转换为任何类型的数组

ios - 防止快速点击 View

php - 添加了 if else 参数和从数据库中丢失的变量

java - SQL Java 使用 phpmyadmin 中参数索引超出范围

ios - 在 iOS 应用程序的 xcode 中存储数千个文件(在包内)

ios - AVMutableComposition的AVPlayer无法播放音频和视频

ios - 是否可以在 NSAttributedString 中混合不同的对齐方式?