如何获取在tableview单元格内的collectionview单元格的按钮单击上的索引

时间:2018-11-22 18:22:41

标签: ios swift uitableview uicollectionview

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

CollectionView单元格

if (Build.VERSION.SDK_INT >= 26) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, 
            CHANNEL_NAME,
            level);
    manager.createNotificationChannel(channel);
}

ViewController

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)
}
}

3 个答案:

答案 0 :(得分:2)

您需要两名代表

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)!
  } 

}

答案 1 :(得分:1)

在IBAction方法中,您可以在sender参数中获取触发按钮。重写您的委托方法以传递按钮和选定的集合视图单元格:

协议SelectedItemCellDelegate:class {     func deleteButton(_ deleteButton:UIButton,tappedInCell单元格: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。如果您可以获取指向表格视图的指针,则可以使用上面的表格视图扩展从表格视图获取按钮的索引路径。

答案 2 :(得分:0)

不鼓励您使用委托并为此目的在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
}