Hello.
I'm seeing some strange behavior with the UITableView and IOS 13 Beta. This code works great in Xcode 10.2.1 and any of the device simulators. But when I run it on Xcode Beta (11M336w) on any device, I get a completely different screen (with the images not constrained to the tableview cell heights!)
Very strange! 🙂
Here is all of the code below. I've also included a link below to an image illustrating the problem between Xcode 10.21 and Xcode Beta.
https://drive.google.com/file/d/1qGaBfPB3NHTF_9tw9117Rpg6rb9XwBFq/view?usp=sharing
class SearchViewController: UIViewController {
private let tips = Tip.dummyData()
private var filteredResults = [(Tip, [SearchResult])]()
private let searchController = UISearchController(searchResultsController: nil)
private let cellID = "CellID"
private let tableView: UITableView = {
let tv = UITableView(frame: .zero, style: .plain)
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
tableView.delegate = self
tableView.dataSource = self
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search "
//navigationItem.searchController = searchController
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = UIColor.black
searchController.searchBar.tintColor = UIColor.white
definesPresentationContext = true
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
setUpLayout()
}
private func setUpLayout() {
view.addSubview(tableView)
tableView.fillSuperview()
}
private func searchBarIsEmpty() -> Bool {
return searchController.searchBar.text?.isEmpty ?? false
}
private func filterResultsForSearchString(_ searchString: String) {
filteredResults.removeAll()
for tip in tips {
let searchResults = tip.searchResultsForSearchString(searchString)
if searchResults.count > 0 {
filteredResults.append((tip, searchResults))
}
}
tableView.reloadData()
}
private func isSearching() -> Bool {
return searchController.isActive && searchBarIsEmpty() == false
}
}
extension SearchViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterResultsForSearchString(searchController.searchBar.text!)
}
}
extension SearchViewController : UITableViewDelegate {
}
extension SearchViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching() {
return filteredResults.count
} else {
return tips.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
let tip: Tip
let text: String
if isSearching() {
let result = filteredResults[indexPath.row]
tip = result.0
text = "(\(tip.id)) " + (result.1.first?.stringValue() ?? "ERROR") + ": " + (result.1.first?.resultText() ?? "ERROR")
} else {
tip = tips[indexPath.row]
text = tip.title.replacingOccurrences(of: "\n", with: " ")
}
cell.textLabel?.text = text
cell.imageView?.image = tip.image
return cell
}
}