Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

ios - UITableView filtering

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

My code is

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

}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

First, create "Person" Model:

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

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...
     //...
}

Note that this is Swift 3 Code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...