UITableView过滤 [英] UITableView filtering

查看:119
本文介绍了UITableView过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在tableview中使用搜索栏执行搜索操作。

其中包含人名的标签以及这些人在其单元格中的图像。

I have to perform a search operation in tableview with searchbar.
Which have a label of person's name and a image for these persons in its cell.

我的代码是

override func viewDidLoad() {
        super.viewDidLoad()

    ArrPersons = ["Mahatma Gandhi","Pramukh Swami","Akshay Kumar","Sachin Tendulkar","Chetan Bhagat","Sardar Vallabhai Patel","Amitabh Bachchan"]
     arrPersonImages = ["1.png","2.png","3.png","4.png","5.png","6.png","7.png"]
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if (searchText.characters.count>0) {
        let predicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)
        ArrPersons = arrTemp
let array = (self.ArrPersons as NSArray).filteredArrayUsingPredicate(predicate)
    print(array)
        ArrPersons = array as! [String]

    }
    else
    {
        ArrPersons = arrTemp
    }
    self.tableviewww.reloadData()

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {

            return self.ArrPersons.count
        }

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableviewww.dequeueReusableCellWithIdentifier("mycell")as! buildVcCell
   cell.personsImages.image = UIImage (named:arrPersonImages[indexPath.row] )
    cell.labelPersonNamess?.text = self.ArrPersons[indexPath.row]
    cell.addBtn.addTarget(self, action: #selector(BuildVc.AddbuttonClicked(_:)), forControlEvents: .TouchUpInside)
    return cell

}

问题是此代码仅对标签人员阵列执行搜索。 arrPersonImages 未按照在搜索栏中输入的人的姓名进行过滤。

The problem is this code only perform search on the array of label persons. arrPersonImages is not filtering according to the name of person entered it the searchbar.

推荐答案

您应该为Person创建一个模型(使用 MVC 模式):

You should create a "Model" for the Person (using MVC pattern):

首先,创建人物模型:

struct Person {
    var name: String?
    var imageName: String?
}

而不是使用两个独立的数组来存储人员的数据,你可以创建一个Person模型数组:

instead of using two separated arrays for storing the persons's data, you can create an array of Person Model:

// add those vars to your ViewController:
var persons = [Person]()
var filteredPersons = [Person]()
var isFiltering = false

override func viewDidLoad() {
    super.viewDidLoad()

    persons = [Person(name: "Ahmad", imageName: "img.png"), Person(name: "Harry", imageName: "img.png")]
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if (searchText.characters.count>0) {
            isFiltering = true
            filteredPersons = persons.filter {
                $0.name?.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
            }
            print(filteredPersons)
        }
        else
        {
            isFiltering = false
            filteredPersons = persons
        }
        self.tableviewww.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return isFiltering == true ? filteredPersons.count : persons.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     //...

     // getting the current person
     let currentPerson = isFiltering == true ? filteredPersons[indexPath.row] : persons[indexPath.row]

     // do the rest of the implementation...
     //...
}

注意这是Swift 3 Code。

Note that this is Swift 3 Code.

这篇关于UITableView过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆