首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在选项卡单元格内的集合视图单元格的按钮上获得索引

如何在选项卡单元格内的集合视图单元格的按钮上获得索引
EN

Stack Overflow用户
提问于 2018-11-22 18:22:40
回答 3查看 3.1K关注 0票数 4

我在collectionView手机里嵌入了一个tableView。CollectionView有多个项,其中包含按钮。集合视图数据源和委托将在UITableViewCell中设置。为此,我必须根据按钮的选择执行一些操作,我需要知道collectionView cell indexPath和tableView cell indexPath。但无法弄清楚,如何实现这一点。尝试使用委托,但不知道如何在委托方法中获取collectionView引用。

CollectionView Cell

代码语言:javascript
运行
复制
protocol SelectedItemCellDelegate:class {
func deleteButtonDidTapped(_ cell: SelectedItemCell)
}
 class SelectedItemCell: UICollectionViewCell {
class var identifier: String{
    return String(describing: self)
}
class var nib: UINib{
    return UINib(nibName: identifier, bundle: nil)
}
@IBOutlet weak var deleteButton: UIButton!
weak var delegate: SelectedItemCellDelegate?
override func awakeFromNib() {
    super.awakeFromNib()
}

@IBAction func deleteAction(_ sender: Any) {
    delegate?.deleteButtonDidTapped(self)
}
}

ViewController

代码语言:javascript
运行
复制
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:SelectedItemCell.identifier, for: indexPath) as! SelectedItemCell
cell.delegate = self                        
return cell
  }

 extension PrescriptionVC: SelectedItemCellDelegate
{   
    func deleteButtonDidTapped(_ cell: SelectedItemCell) 
    {
      // Need tableview indexPath as well SelectedItemCell indexPath.
    }
}
EN

Stack Overflow用户

回答已采纳

发布于 2018-11-23 09:03:53

不鼓励您在Swift中使用委托和查看数学来实现这个目的,。使用简单的回调闭包

在单元格中,删除与协议相关的代码,声明闭包,并在按下按钮时调用它。

代码语言:javascript
运行
复制
class SelectedItemCell: UICollectionViewCell {
    class var identifier: String{
        return String(describing: self)
    }
    class var nib: UINib{
        return UINib(nibName: identifier, bundle: nil)
    }
    @IBOutlet weak var deleteButton: UIButton!

    var callback : (() -> Void)?

    @IBAction func deleteAction(_ sender: Any) {
        callback?()
    }
}

在控制器中,设置闭包并处理回调,捕获索引路径。

代码语言:javascript
运行
复制
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier:SelectedItemCell.identifier, for: indexPath) as! SelectedItemCell
    cell.callback = {
        print("button pressed", indexPath)
    }                        
    return cell
}

如果可以插入、删除或移动仅捕获cellForItemAt中的cellForItemAt的项,则无法工作,因为索引路径可以更改而无需调用cellForItemAt。在这种情况下,您必须在闭包中传递单元格并获得实际的索引路径。

代码语言:javascript
运行
复制
var callback : ((UICollectionViewCell) -> Void)?

@IBAction func deleteAction(_ sender: Any) {
    callback?(self)
}

代码语言:javascript
运行
复制
cell.callback = { currentCell in
   print("button pressed", collectionView.indexPath(for: currentCell)!
} 
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53436418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档