【问题标题】:How can i access the tag of button in CollectionView's cell contained in TableView cell如何访问 TableView 单元格中包含的 CollectionView 单元格中的按钮标签
【发布时间】:2017-07-05 04:55:23
【问题描述】:

我正在做一个项目,其中我在UITableViewCell. 中有一个UICollectionView 我的视图层次结构如下:

  • 表格视图单元格
    • 集合视图
      • 集合查看单元格
        • 按钮(我想访问知道 tagValue)。

我有两个表格视图单元格,每个 tableView 单元格中有一个 UICollectionView 并且每个 UICollectionView 包含 4 个 UICollectionViewCell,所以我想知道我的按钮的确切 tagValue 包含在 UICollectionViewCell 中被按下。 我还为 cellForItem atIndexpath TableView 单元格类中的按钮提供了标签值,就像这样

cell.button.tag = indexpath.item

我还为按钮提供了选择器。

【问题讨论】:

  • 您想从哪里访问该集合视图单元?我的意思是来自哪个类和方法?
  • 你的标签应该是这样的 -> (tableviewcell.indexpath.row + 1)*100 + colCell..indexpath.row + 1
  • @Lion 我正在从表格视图单元格类内部访问 collectionView 单元格。
  • 其实你应该已经把数据从tableview的cellforrow传到collection view了!这样您就可以在集合视图委托中访问该数据!
  • 我是 ios 开发的新手,这就是为什么我不能将数据从 tableView 的 cellForRow 传递给 collectionView .. 你能建议我这样做吗?或者只是给我一个 ...cellForRow 方法的示例代码。

标签: ios objective-c swift tableview collectionview


【解决方案1】:

试试这个:

删除这一行:

cell.button.tag = indexpath.item

添加此功能

 @IBAction func clickCollectionCellButton(sender : UIButton){
            var cell: UICollectionViewCell? = (sender.superview?.superview as? UICollectionViewCell) //track your view hierarchy
            var indexpath: IndexPath? = collectionView?.indexPath(for: cell!)
           // do your additional work
        }

【讨论】:

  • 我会在哪个班级做这个动作。
  • 在你需要点击按钮的那个班级。
【解决方案2】:

无需分配标签值,您可以直接获取按钮中的单元格索引路径存在

假设你像这样为你的按钮分配一个选择器:-

@IBAction function buttonAction(_ sender:UIBUtton){
      if let indexPath = viewIndexPathInCollectionView(sender , collView:pass your collectionView here){
         // here you will get indexPath of your collectionView Cell
         print(indexPath.item)  
      } 
}

使用此函数通过在此函数中传递单元格项目来获取集合视图单元格的索引

func viewIndexPathInCollectionView(_ cellItem: UIView, collView: UICollectionView) -> IndexPath? {
    let pointInTable: CGPoint = cellItem.convert(cellItem.bounds.origin, to: collView)
    if let cellIndexPath = collView.indexPathForItem(at: pointInTable){
        return cellIndexPath
    }
    return nil

}

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 2016-12-20
    • 2020-10-20
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多