Stack Overflow Asked on December 11, 2021
I have a UITableView inside of a UIScrollView. The UITableView has scrolling disabled and has all the required delgates within the ViewController (as can be seen in the code below). However, when I click an item in the table, didSelectRowAt is never called (didHighlightRowAt is also never called). Why is it not called? How do I fix it?
class NewsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private static let headlineArticleReuseIdentifier = "HeadlineArticleCell"
private static let categoryCellReuseIdentifier = "NewsCategoryCell"
@IBOutlet weak var headlineArticlesPreviewList: UITableView!
private var bindings = Set<AnyCancellable>()
private var viewModel: NewsViewModel = NewsViewModel()
private var headlineArticles: [Article] = []
@IBOutlet weak var headlineTitle: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationController?.setNavigationBarHidden(true, animated: false)
headlineArticlesPreviewList.delegate = self
headlineArticlesPreviewList.dataSource = self
let cancellable = viewModel.$viewState.sink(receiveValue: { state in
switch state {
case let .data(data):
self.setData(data: data)
print()
case let .error(error):
print(error)
case .loading:
print()
}
})
bindings.insert(cancellable)
}
private func setData(data: NewsViewModel.ViewState.Data) {
self.headlineArticles = data.headlineArticles
self.headlineTitle.text = data.headlineCategory.displayName
self.headlineArticlesPreviewList.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return headlineArticles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = headlineArticlesPreviewList.dequeueReusableCell(
withIdentifier: NewsViewController.headlineArticleReuseIdentifier,
for: indexPath)
as? HeadlineArticleTableViewCell else {
fatalError("could not cast to headline article")
}
let headlineArticle = headlineArticles[indexPath.item]
cell.setArticle(article: headlineArticle)
return cell
}
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
print()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "HeadlineArticleSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HeadlineArticleSegue" {
let detailViewController = segue.destination
as! ArticleViewController
let articleIndexPath = headlineArticlesPreviewList.indexPathForSelectedRow!
let row = articleIndexPath.row
detailViewController.article = headlineArticles[row]
}
}
}
UITableView Settings
UITableView Inside UIScrollView
UITableViewCell Settings
Answered by rarora25 on December 11, 2021
From my perspective this layout of elements in storyboard is not good.
I would suggest you to use 2 embedded views, 1 for tableview and other for collection view and use delegates on handling the didselect row for tableview and collectionview in those embedded view controllers and then use protocols and delegates to inform main view controller to do specific tasks.
With this approach your current view controller will be light and will do only specific tasks and will delegate rendering of table view and collection view to respective embedded view controllers.
Answered by Ankit Thakur on December 11, 2021
I had a storyboard segue set that had an incorrect restoration ID
Answered by Adrian Le Roy Devezin on December 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP