IOS Swift - filtering UITableView content with a custom cell using the search bar and search display

I have a problem with the search bar and the search display in the Swift project for iPhone. I added one UITableView with custom cells to the view. The cell set the QuestionCell parameter to Identifier, and the Class to QuestionCellTableViewCell. I added two labels to this cell, and I added outputs for shortcuts to the cell class. My goal is to filter the contents of a UITableView.

This is my ViewController: DomandeViewController.swift

import UIKit

class DomandeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate {

    var arrayDomande = [Domanda]()
    var areaDomande: Area = Area()
    var argomentoDomande: Argomento = Argomento()
    var domandeFiltrate = [Domanda]()

    @IBOutlet weak var questionTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        arrayDomande = ModelManager.instance.getQuestionsWithArguments(inAreaId: areaDomande.id, aboutId: argomentoDomande.id)
        self.searchDisplayController!.searchResultsTableView.registerClass(QuestionCellTableViewCell.classForCoder(), forCellReuseIdentifier: "QuestionCell")
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        if(tableView == self.searchDisplayController!.searchResultsTableView){
            return 1
        }else{
            return 1
        }
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            return domandeFiltrate.count
        } else {
            return arrayDomande.count
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell

        var domanda = Domanda()

        if tableView == searchDisplayController!.searchResultsTableView {
            domanda = domandeFiltrate[indexPath.row]
        } else {
            domanda = arrayDomande[indexPath.row]
        }

        cell.testoLabel.text = domanda.testo
        cell.numeroLabel.text = String(domanda.numero)

        return cell
    }

    func filterContentForSearchText(searchText: String) {
        // Filter the array using the filter method
        filteredDomande = arrayDomande.filter({( domanda: Domanda) -> Bool in
            let stringMatch = domanda.testo.rangeOfString(searchText)
            return stringMatch != nil
        })
    }

    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
        self.filterContentForSearchText(searchString)
        return true
    }
}

This is my QuestionCellTableViewCell.swift

import UIKit

class QuestionCellTableViewCell: UITableViewCell {

    @IBOutlet weak var testoLabel: UILabel!
    @IBOutlet weak var numeroLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

And this is my Domanda.swift

import UIKit

class Domanda: NSObject {
    var numero: Int = Int()
    var testo: String = String()
    var preferita: Bool = Bool()
    var visualizzato: Int = Int()
    var area: Int = Int()
    var argomento: Int = Int()
}

, : :

" : "

, "", (testoLabel numeroLabel) ,

cell.testoLabel.text = domanda.testo

viewDidLoad()

self.searchDisplayController!.searchResultsTableView.registerClass(QuestionCellTableViewCell.classForCoder(), forCellReuseIdentifier: "QuestionCell")

var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell

viewDidLoad()

self.questionTableView.registerClass(QuestionCellTableViewCell.classForCoder(), forCellReuseIdentifier: "QuestionCell")

cell.testoLabel.text, .

? , !

Andrea

EDIT:

, : searchResultsTableView , , , . :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell
        var domanda = Domanda()

        if tableView == self.searchDisplayController!.searchResultsTableView {
            domanda = filteredDomande[indexPath.row]

            cell.textLabel.text = domanda.testo
        } else {
            domanda = arrayDomande[indexPath.row]

            cell.testoLabel.text = domanda.testo
            cell.numeroLabel.text = String(domanda.numero)
        }

        return cell
    }

, searchResultsTableView. ? !

+4
1

. , . 0 questionTableView, Nib forCellReuseIdentifier viewDidLoad() questionTableView, searchResultsTableView. :

var cellIdentifier = "questionCell"

@IBOutlet weak var questionTableView: UITableView!

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        arrayDomande = ModelManager.instance.getQuestionsWithArguments(inAreaId: areaDomande.id, aboutId: argomentoDomande.id)

        var nib = UINib(nibName: "QuestionCellTableViewCell", bundle: nil)
        self.searchDisplayController!.searchResultsTableView.registerNib(nib, forCellReuseIdentifier: cellIdentifier)
        self.questionTableView.registerNib(nib, forCellReuseIdentifier: cellIdentifier)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: QuestionCellTableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as QuestionCellTableViewCell
        var domanda = Domanda()

        if tableView == self.searchDisplayController!.searchResultsTableView {
            domanda = filteredDomande[indexPath.row]
        } else {
            domanda = arrayDomande[indexPath.row]
        }
        cell.testoLabel.text = domanda.testo
        cell.numeroLabel.text = String(domanda.numero)

        return cell
    }
+3

All Articles