如何从SearchBar获取Tableview中所有项目的当前项目的索引

嫁给G

我正在尝试从Tableview中删除一个项目,如果SearchBar为空,则一切正常,(indexPath是正确的索引)。但是如果我使用SearchBar,我会得到过滤项的索引。

如何从Tableview获取项目的当前索引?但在使用SearchBar过滤后。

 func filterTableView(ind:Int,text:String) {
        switch ind {
        case selectedScope.reviewDate.rawValue:
            filteredDays = ViewController.reviewDaysArr.filter({ (mod) -> Bool in
                return mod.reviewDate.lowercased().contains(text.lowercased())
            })
            self.tableView.reloadData()

        case selectedScope.reviewPlace.rawValue:
            filteredDays = ViewController.reviewDaysArr.filter({ (mod) -> Bool in
                return mod.reviewPlace.lowercased().contains(text.lowercased())
            })
            self.tableView.reloadData()

        case selectedScope.reviewSum.rawValue:
            filteredDays = ViewController.reviewDaysArr.filter({ (mod) -> Bool in
                return mod.reviewSum.lowercased().contains(text.lowercased())
            })
            self.tableView.reloadData()

        }
    }


static var reviewDaysArr:[ReviewDay] = [

    ReviewDay(reviewDate: "03/03/2017", reviewPlace: "Manhattan"),
    ReviewDay(reviewDate: "03/03/2017", reviewPlace: "Manhattan"),
    ReviewDay(reviewDate: "05/03/2017", reviewPlace: "New Jersey")

]

var filteredDays = reviewDaysArr

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        ViewController.reviewDaysArr.remove(at: indexPath.row)
        filteredDays = ViewController.reviewDaysArr
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}
尼拉夫D

您可以从过滤后的数组中获取主数组的索引。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        //First check using your condition that is search active 
        if searchActive {
            //I'm comparing movieId you can compare any unique property of your Movie class
            if let index = ViewController.dataArr.index(where: {$0.movieId == filterdData[indexPath.row].movieId}) {
                //Remove object from mainArray
                ViewController.dataArr.remove(at: index)
                //Remove object from filtered array
                filterdData.remove(at: indexPath.row)
            }
        }
        else {
            ViewController.dataArr.remove(at: indexPath.row)
        }
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

编辑:由于您的模型类没有任何唯一属性,Movie因此只有一种获取对象索引的方法。您需要获取与主数组相对应的已过滤数组的索引数组。因此,声明一个类型为数组的数组,[Int]并在对数组进行过滤后获得该数组中的过滤器对象索引。

var filterIndices = [Int]()

现在,您在过滤数组的地方也初始化了该数组。

switch ind {
    case selectedScope.reviewDate.rawValue:
        filteredDays = ViewController.reviewDaysArr.filter({ (mod) -> Bool in
            return mod.reviewDate.lowercased().contains(text.lowercased())
        })
        filterIndices = ViewController.reviewDaysArr.indices.filter({
            ViewController.reviewDaysArr[$0].reviewDate.lowercased().contains(text.lowercased()) 
        })
        self.tableView.reloadData()

    case selectedScope.reviewPlace.rawValue:
        filteredDays = ViewController.reviewDaysArr.filter({ (mod) -> Bool in
            return mod.reviewPlace.lowercased().contains(text.lowercased())
        })
        filterIndices = ViewController.reviewDaysArr.indices.filter({
            ViewController.reviewDaysArr[$0].reviewPlace.lowercased().contains(text.lowercased()) 
        })
        self.tableView.reloadData()

    case selectedScope.reviewSum.rawValue:
        filteredDays = ViewController.reviewDaysArr.filter({ (mod) -> Bool in
            return mod.reviewSum.lowercased().contains(text.lowercased())
        })
        filterIndices = ViewController.reviewDaysArr.indices.filter({
            ViewController.reviewDaysArr[$0].reviewSum.lowercased().contains(text.lowercased()) 
        })
        self.tableView.reloadData()
}

现在,commit editingStyle获取该索引表单数组,并从main和filter数组中删除该对象。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        //First check using your condition that is search active 
        if searchActive {
            //Remove object from mainArray using filterIndices
            ViewController.reviewDaysArr.remove(at: filterIndices[indexPath.row])
            //Remove object from filtered array
            filteredDays.remove(at: indexPath.row)
        }
        else {
            ViewController.reviewDaysArr.remove(at: indexPath.row)
        }
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何更改RecyclerView中所有项目的布局?

如何获取嵌套数组中所有非嵌套项目的长度?

如何获取Gradle项目的所有配置的列表?

Delphi-在TListView中获取所选项目的当前索引

如何获取SortedSet中项目的索引

JavaScript数组映射函数如何确定当前项目的长度和索引

如何获取试剂中项目的索引

在Eclipse插件中获取当前项目的通用方法

如何在Delphi 10.2中使用ToolsAPI获取当前项目的版本号

获取当前项目的根文件夹

Vue v-用于检索当前项目的索引

如何获取包含Paper.js中所有项目的边界框?

获取数据网格中所选项目的行索引

获取Javascript中每个箭头函数的当前项目的索引

如何在for_each循环中检索当前项目的索引?

使用Object.entries()。map获取项目的当前索引

如何获取IntelliJ插件的当前项目目录

当前项目的CSS内容属性

Doctrine2获取集合中当前项目的索引

如何跟踪某个项目的当前观看者数量?

获取数据表中所有项目的ID

如何有效获取列表中所有项目的索引中的序数位置

JQuery - 项目的当前表行

如何将外部 Maven 项目的依赖项添加到当前项目?

Sharepoint 设计器不会在工作流中显示当前项目的所有字段

当前项目的 WPF 动态资源

如何在 Google Cloud Platform 中获取当前项目的组织 ID?

获取列表苹果脚本中所选项目的索引

如何使用数组获取对象中所有项目的总长度