TableView displaying cells uncorrectly

52 Views Asked by At

Have a property

var resultSearchController = UISearchController()

Creating UISearchController on viewDidLoad() method

        self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.barStyle = UIBarStyle.black
        controller.searchBar.barTintColor = UIColor.white
        controller.searchBar.backgroundColor = UIColor.clear
        controller.hidesNavigationBarDuringPresentation = false
        self.tableView.tableHeaderView = controller.searchBar
        return controller
    })()

As result have a note text with normal displaying plus and particularly displaying a start on text on left top corner of each cell. What could i do wrong? P.S. Figured out most likely it have nothing to do with UISearchBar

enter image description here

2

There are 2 best solutions below

0
Ivan Zolotarov On BEST ANSWER

I found the lines i made a mistake. I used property cell.textLabel instead of cell.noteLabel. Ty for your time.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "NoteTableViewCell"

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! NoteTableViewCell
    let note: Note
    if self.resultSearchController.isActive {
        note = filteredNotes[indexPath.row]
        cell.textLabel?.text = filteredNotes[indexPath.row].note
    } else {
        note = notes[indexPath.row]
        cell.textLabel?.text = notes[indexPath.row].note
    }

    return cell
}
0
MacUserT On

I'm not familiar with how you create the resultSearchController, since I have never seen such a construction. However, that might be my limited knowledge. I would create the resultSearchController a bit differently with a more likely successful result.

lazy var resultSearchController : UISearchController = {
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.barStyle = UIBarStyle.black
        controller.searchBar.barTintColor = UIColor.white
        controller.searchBar.backgroundColor = UIColor.clear
        controller.hidesNavigationBarDuringPresentation = false
        self.tableView.tableHeaderView = controller.searchBar
        return controller
}()

This is the correct way to lazily initialize a variable that will be created upon use and get all the settings you defined.

If this doesn't work it is not the definition and initialization that is the problem and you should show some more code where it might go wrong.