【问题标题】:CollectionView Issue with UIButtonUIButton 的 CollectionView 问题
【发布时间】:2018-12-07 12:24:12
【问题描述】:

我正在使用CollectionViewUIButton。我的代码面临一些问题。我想在按钮单击时访问collectionViewCell。按钮位于collectionView 之外。 那么,我该如何访问它呢?

if let cell = (sender as? UIButton)?.superview?.superview?.superview as? FavouriteVideosCell
    {
        let index = collectionview.indexPath(for: cell)
        let video_id = arrayFavouriteVideo[(index?.row)!].video_id
        RemoveVideoAPI(video_id: video_id)

    }

【问题讨论】:

  • 在此分享任何截图
  • @ktrkathir 实际上我的按钮在 collectionview 之外,我想从单元格访问 Video_ID。
  • @ktrkahir 方法是正确的。您没有被清除为视图层次结构之外或尚未出队或可见。
  • 能否分享一些有用的细节
  • 如果是后者则无法点击。否则,您可以使用 cellForRow() 方法通过索引访问该单元格。

标签: swift uicollectionview uibutton


【解决方案1】:

这样试试

if let indexPath = collectionview.indexPath(for: cell) {

  let videoID = arrayFavouriteVideo[indexPath.item].video_id
  RemoveVideoAPI(video_id: videoID)
}

【讨论】:

    【解决方案2】:

    您的方法完全错误,这里没有遵循任何应用架构。 但如果你只想要videoID。

    最简单的方法是在你的collectionView cellForItem 方法中设置按钮上的标签。并使用按钮上的标签属性访问它,而无需访问单元格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多