didSelectRowAt не вызывается для UITableView внутри UIScrollView - PullRequest
0 голосов
/ 13 июля 2020

У меня есть UITableView внутри UIScrollView. У UITableView отключена прокрутка и есть все необходимые delgates внутри ViewController (как можно увидеть в приведенном ниже коде). Однако, когда я щелкаю элемент в таблице, didSelectRowAt никогда не вызывается (didHighlightRowAt также никогда не вызывается). Почему не называется? Как это исправить?

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

UITableView Inside UIScrollView enter image description here

UITableViewCell Settings

введите описание изображения здесь

Ответы [ 3 ]

0 голосов
/ 22 июля 2020
• 1000 и collectionview в этих встроенных контроллерах представления, а затем использовать протоколы и делегатов, чтобы сообщить контроллеру основного представления о выполнении определенных c задач.

При таком подходе ваш текущий контроллер представления будет легким и будет выполнять только определенные c задач и делегирует отрисовку табличного представления и представления коллекции соответствующим встроенным контроллерам представления.

0 голосов
/ 23 июля 2020
  • набор переходов раскадровки с неверным идентификатором восстановления
0 голосов
/ 17 июля 2020

У меня был набор переходов для раскадровки с неверным идентификатором восстановления

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...