-1

我在FooterView中设置了一个按钮,它属于一个collectionView,它在一个TableView中。如何从此按钮获取 indexPath.section?

这是一个新的分段控件的选项卡。过去,我尝试将 indexPath.section 分配给 int 变量,甚至将数字分配给常量,在单元格中使用 prepareForReuse 方法来更新单元格,但它总是给我错误的索引。当我滚动时它会改变。

var currentlyActiveCellIndexPath: Int?

extension LibraryViewController {
    func viewTapped(button: UIButton) {

        let tapgesture = UITapGestureRecognizer.init(target: self, action: #selector(goToViewAllArticles))
        button.addGestureRecognizer(tapgesture)

        let tapgesture1 = UITapGestureRecognizer.init(target: self, action: #selector(goToViewAllPodcasts))
        button.addGestureRecognizer(tapgesture1)



        tapgesture.numberOfTapsRequired = 1

        button.isUserInteractionEnabled = true



    }
    @objc func goToViewAllArticles(sender: UITapGestureRecognizer) {
        return self.performSegue(withIdentifier: "showBrowseFromButton", sender: currentlyActiveCellIndexPath)
    }

    @objc func goToViewAllPodcasts(sender: UITapGestureRecognizer) {
        return self.performSegue(withIdentifier: "showBrowseFromButton", sender: currentlyActiveCellIndexPath)
    }


  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {


        currentlyActiveCellIndexPath = indexPath.section

我希望 indexPath.section 保持不变,但是一旦滚动,它就会改变,我无法获得正确的 indexPath.section。

4

1 回答 1

0

将按钮的标签设置为 indexPath

在 cellForRowAt(:)

YourBtn.tag = indexPath.row

现在,当您点击该按钮时,如果仍然无法正常工作,您可以获得完全相同的 indexPath 并发表评论!

于 2019-08-29T12:56:49.283 回答