我在collectionView手机里嵌入了一个tableView。CollectionView有多个项,其中包含按钮。集合视图数据源和委托将在UITableViewCell中设置。为此,我必须根据按钮的选择执行一些操作,我需要知道collectionView cell indexPath和tableView cell indexPath。但无法弄清楚,如何实现这一点。尝试使用委托,但不知道如何在委托方法中获取collectionView引用。
CollectionView Cell
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
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.
    }
}发布于 2018-11-23 09:03:53
不鼓励您在Swift中使用委托和查看数学来实现这个目的,。使用简单的回调闭包
在单元格中,删除与协议相关的代码,声明闭包,并在按下按钮时调用它。
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?()
    }
}在控制器中,设置闭包并处理回调,捕获索引路径。
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。在这种情况下,您必须在闭包中传递单元格并获得实际的索引路径。
var callback : ((UICollectionViewCell) -> Void)?
@IBAction func deleteAction(_ sender: Any) {
    callback?(self)
}和
cell.callback = { currentCell in
   print("button pressed", collectionView.indexPath(for: currentCell)!
} 发布于 2018-11-22 20:42:25
你需要两个代表
protocol selectCollectionCellDelegate {
 func selectCell(cell : UICollectionViewCell )     
}
protocol selectTableCellDelegate {
 func selectTableCell(cell : UITableViewCell , indexPath : IndexPath )
}
class YourCollectionViewCell : UICollectionViewCell {
  var tvcDelegate : selectCollectionCellDelegate
@IBAction func deleteAction(_ sender: Any) {
    tvcDelegate.selectCell(cell : self)
 }
}
class YourTableViewCell : UITableViewCell , selectCollectionCellDelegate {
  var vcDelegate : selectTableCellDelegate
 func selectCell(cell : UICollectionViewCell ){
     let indexPath : IndexPath = collectionView.indexPath(for: cell)!
     delegate.selectTableCell(cell : self , indexPath : indexPath  )
  } 
}
class YourviewController : UIViewController , selectTableCellDelegate{
 func selectTableCell(cell : UITableViewCell , indexPath : IndexPath){
   //indexPatn is IndexPath of collectionViewCell
   let tableCellindexPath : IndexPath = tableView.indexPath(for: self)!
  } 
}发布于 2018-11-22 20:12:26
在IBAction方法中,可以获得发送方参数中的触发按钮。重写委托方法以传递按钮和选定的集合视图单元格:
协议选择SelectedItemCellDelegate:class { func deleteButton(_ deleteButton: UIButton,tappedInCell cell: SelectedItemCell) }
重写deleteAction以将发件人传递为UIButton类(或任何UIView类)
@IBAction func deleteAction(_ sender: UIButton) {
    delegate?. deleteButton(sender, tappedInCell: self)
}然后,可以向UICollectionView和UITableView添加扩展,以便使用按钮的坐标计算出包含按钮的单元格:
extension UICollectionView {
    func indexPathForCellContaining( view: UIView) -> IndexPath? {
        let viewCenter = self.convert(view.center, from: view.superview)
        return self.indexPathForItem(at: viewCenter)
    }
}或表视图:
public extension UITableView {
    /**
     This method returns the indexPath of the cell that contains the specified view
     - Parameter view: The view to find.
     - Returns: The indexPath of the cell containing the view, or nil if it can't be found
     */
    func indexPathForView(_ view: UIView) -> IndexPath? {
        let center = view.center
        //The center of the view is a better point to use, but we can only use it if the view has a superview
        guard let superview = view.superview else {
            //The view we were passed does not have a valid superview.
            //Use the view's bounds.origin and convert from the view's coordinate system
            let origin = self.convert(view.bounds.origin, from: view)
            let indexPath = self.indexPathForRow(at: origin)
            return indexPath
        }
        let viewCenter = self.convert(center, from: superview)
        let indexPath = self.indexPathForRow(at: viewCenter)
        return indexPath
    }
}由于您已经将集合视图单元格传递给委托,所以可以使用func indexPath(for cell: UICollectionViewCell)获取CollectionView单元格的indexPath。如果可以获得指向表视图的指针,则可以使用上面的表视图扩展从表视图中获取按钮的索引路径。
https://stackoverflow.com/questions/53436418
复制相似问题