How to get the indexpath.row when an element is activated?

前端 未结 19 2172
梦如初夏
梦如初夏 2020-11-21 23:30

I have a tableview with buttons and I want to use the indexpath.row when one of them is tapped. This is what I currently have, but it always is 0

var point =         


        
相关标签:
19条回答
  • 2020-11-21 23:46

    I used convertPoint method to get point from tableview and pass this point to indexPathForRowAtPoint method to get indexPath

     @IBAction func newsButtonAction(sender: UIButton) {
            let buttonPosition = sender.convertPoint(CGPointZero, toView: self.newsTableView)
            let indexPath = self.newsTableView.indexPathForRowAtPoint(buttonPosition)
            if indexPath != nil {
                if indexPath?.row == 1{
                    self.performSegueWithIdentifier("alertViewController", sender: self);
                }   
            }
        }
    
    0 讨论(0)
  • 2020-11-21 23:46

    In Swift 3. Also used guard statements, avoiding a long chain of braces.

    func buttonTapped(sender: UIButton) {
        guard let cellInAction = sender.superview as? UITableViewCell else { return }
        guard let indexPath = tableView?.indexPath(for: cellInAction) else { return }
    
        print(indexPath)
    }
    
    0 讨论(0)
  • 2020-11-21 23:49

    In my case i have multiple sections and both the section and row index is vital, so in such a case i just created a property on UIButton which i set the cell indexPath like so:

    fileprivate struct AssociatedKeys {
        static var index = 0
    }
    
    extension UIButton {
    
        var indexPath: IndexPath? {
            get {
                return objc_getAssociatedObject(self, &AssociatedKeys.index) as? IndexPath
            }
            set {
                objc_setAssociatedObject(self, &AssociatedKeys.index, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
    

    Then set the property in cellForRowAt like this :

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! Cell
        cell.button.indexPath = indexPath
    }
    

    Then in the handleTapAction you can get the indexPath like this :

    @objc func handleTapAction(_ sender: UIButton) {
        self.selectedIndex = sender.indexPath
    
    }
    
    0 讨论(0)
  • 2020-11-21 23:49

    Swift 4 and 5

    Method 1 using Protocol delegate

    For example, you have a UITableViewCell with name MyCell

    class MyCell: UITableViewCell {
        
        var delegate:MyCellDelegate!
        
        @IBAction private func myAction(_ sender: UIButton){
            delegate.didPressButton(cell: self)
        }
    }
    

    Now create a protocol

    protocol MyCellDelegate {
        func didPressButton(cell: UITableViewCell)
    }
    

    Next step, create an Extension of UITableView

    extension UITableView {
        func returnIndexPath(cell: UITableViewCell) -> IndexPath?{
            guard let indexPath = self.indexPath(for: cell) else {
                return nil
            }
            return indexPath
        }
    }
    

    In your UIViewController implement the protocol MyCellDelegate

    class ViewController: UIViewController, MyCellDelegate {
         
        func didPressButton(cell: UITableViewCell) {
            if let indexpath = self.myTableView.returnIndexPath(cell: cell) {
                  print(indexpath)
            }
        }
    }
    

    Method 2 using closures

    In UIViewController

    override func viewDidLoad() {
            super.viewDidLoad()
           //using the same `UITableView extension` get the IndexPath here
            didPressButton = { cell in
                if let indexpath = self.myTableView.returnIndexPath(cell: cell) {
                      print(indexpath)
                }
            }
        }
    
     var didPressButton: ((UITableViewCell) -> Void)
    
    class MyCell: UITableViewCell {
    
        @IBAction private func myAction(_ sender: UIButton){
            didPressButton(self)
        }
    }
    

    Note:- if you want to get UICollectionView indexPath you can use this UICollectionView extension and repeat the above steps

    extension UICollectionView {
        func returnIndexPath(cell: UICollectionViewCell) -> IndexPath?{
            guard let indexPath = self.indexPath(for: cell) else {
                return nil
            }
            return indexPath
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:52

    giorashc almost had it with his answer, but he overlooked the fact that cell's have an extra contentView layer. Thus, we have to go one layer deeper:

    guard let cell = sender.superview?.superview as? YourCellClassHere else {
        return // or fatalError() or whatever
    }
    
    let indexPath = itemTable.indexPath(for: cell)
    

    This is because within the view hierarchy a tableView has cells as subviews which subsequently have their own 'content views' this is why you must get the superview of this content view to get the cell itself. As a result of this, if your button is contained in a subview rather than directly into the cell's content view, you'll have to go however many layers deeper to access it.

    The above is one such approach, but not necessarily the best approach. Whilst it is functional, it assumes details about a UITableViewCell that Apple have never necessarily documented, such as it's view hierarchy. This could be changed in the future, and the above code may well behave unpredictably as a result.

    As a result of the above, for longevity and reliability reasons, I recommend adopting another approach. There are many alternatives listed in this thread, and I encourage you to read down, but my personal favourite is as follows:

    Hold a property of a closure on your cell class, have the button's action method invoke this.

    class MyCell: UITableViewCell {
        var button: UIButton!
    
        var buttonAction: ((Any) -> Void)?
    
        @objc func buttonPressed(sender: Any) {
            self.buttonAction?(sender)
        }
    }
    

    Then, when you create your cell in cellForRowAtIndexPath, you can assign a value to your closure.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCell
        cell.buttonAction = { sender in
            // Do whatever you want from your button here.
        }
        // OR
        cell.buttonAction = buttonPressed(closure: buttonAction, indexPath: indexPath) // <- Method on the view controller to handle button presses.
    }
    

    By moving your handler code here, you can take advantage of the already present indexPath argument. This is a much safer approach that the one listed above as it doesn't rely on undocumented traits.

    0 讨论(0)
  • 2020-11-21 23:52
    // CustomCell.swift
    
    protocol CustomCellDelegate {
        func tapDeleteButton(at cell: CustomCell)
    }
    
    class CustomCell: UICollectionViewCell {
        
        var delegate: CustomCellDelegate?
        
        fileprivate let deleteButton: UIButton = {
            let button = UIButton(frame: .zero)
            button.setImage(UIImage(named: "delete"), for: .normal)
            button.addTarget(self, action: #selector(deleteButtonTapped(_:)), for: .touchUpInside)
            button.translatesAutoresizingMaskIntoConstraints = false
            return button
        }()
        
        @objc fileprivate func deleteButtonTapped(_sender: UIButton) {
            delegate?.tapDeleteButton(at: self)
        }
        
    }
    
    //  ViewController.swift
    
    extension ViewController: UICollectionViewDataSource {
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: customCellIdentifier, for: indexPath) as? CustomCell else {
                fatalError("Unexpected cell instead of CustomCell")
            }
            cell.delegate = self
            return cell
        }
    
    }
    
    extension ViewController: CustomCellDelegate {
    
        func tapDeleteButton(at cell: CustomCell) {
            // Here we get the indexPath of the cell what we tapped on.
            let indexPath = collectionView.indexPath(for: cell)
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题