2016-11-21 140 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