2016-11-21 127 views
0

有一个属性的TableView显示细胞uncorrectly

var resultSearchController = UISearchController() 

在viewDidLoad中创建UISearchController()方法

 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 
    })() 

至于结果与正常的一个注释文本显示加,特别是显示文本开始左每个单元的顶部角落。 我能做什么错? P.S.想出最有可能什么都没有做的UISearchBar

enter image description here

回答

0

我发现了我犯的一个错误。我用财产cell.textLabel而不是cell.noteLabel。 Ty为你的时间。

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

1

我不熟悉如何创建resultSearchController,因为我从来没有见过这样的结构。但是,那可能是我有限的知识。我会创建resultSearchController有点不同,更可能成功的结果。

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 
}() 

这是一种懒惰初始化一个变量的正确方法,它将在使用后创建并获得您定义的所有设置。

如果这不起作用,它不是问题的定义和初始化,你应该在可能出错的地方显示更多的代码。

+0

TY此建议 – L1GhTUA

+0

是否现在的工作? – MacUserT

+0

是的,看我的回答 – L1GhTUA