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

前端 未结 19 2174
梦如初夏
梦如初夏 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:55

    Use an extension to UITableView to fetch the cell for any view:


    @Paulw11's answer of setting up a custom cell type with a delegate property that sends messages to the table view is a good way to go, but it requires a certain amount of work to set up.

    I think walking the table view cell's view hierarchy looking for the cell is a bad idea. It is fragile - if you later enclose your button in a view for layout purposes, that code is likely to break.

    Using view tags is also fragile. You have to remember to set up the tags when you create the cell, and if you use that approach in a view controller that uses view tags for another purpose you can have duplicate tag numbers and your code can fail to work as expected.

    I have created an extension to UITableView that lets you get the indexPath for any view that is contained in a table view cell. It returns an Optional that will be nil if the view passed in actually does not fall within a table view cell. Below is the extension source file in it's entirety. You can simply put this file in your project and then use the included indexPathForView(_:) method to find the indexPath that contains any view.

    //
    //  UITableView+indexPathForView.swift
    //  TableViewExtension
    //
    //  Created by Duncan Champney on 12/23/16.
    //  Copyright © 2016-2017 Duncan Champney.
    //  May be used freely in for any purpose as long as this 
    //  copyright notice is included.
    
    import UIKit
    
    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
            let viewCenter = self.convert(center, from: view.superview)
            let indexPath = self.indexPathForRow(at: viewCenter)
            return indexPath
        }
    }
    

    To use it, you can simply call the method in the IBAction for a button that's contained in a cell:

    func buttonTapped(_ button: UIButton) {
      if let indexPath = self.tableView.indexPathForView(button) {
        print("Button tapped at indexPath \(indexPath)")
      }
      else {
        print("Button indexPath not found")
      }
    }
    

    (Note that the indexPathForView(_:) function will only work if the view object it's passed is contained by a cell that's currently on-screen. That's reasonable, since a view that is not on-screen doesn't actually belong to a specific indexPath; it's likely to be assigned to a different indexPath when it's containing cell is recycled.)

    EDIT:

    You can download a working demo project that uses the above extension from Github: TableViewExtension.git

    0 讨论(0)
  • 2020-11-21 23:55

    Since the sender of the event handler is the button itself, I'd use the button's tag property to store the index, initialized in cellForRowAtIndexPath.

    But with a little more work I'd do in a completely different way. If you are using a custom cell, this is how I would approach the problem:

    • add an 'indexPath` property to the custom table cell
    • initialize it in cellForRowAtIndexPath
    • move the tap handler from the view controller to the cell implementation
    • use the delegation pattern to notify the view controller about the tap event, passing the index path
    0 讨论(0)
  • 2020-11-21 23:56

    Sometimes button may be inside of another view of UITableViewCell. In that case superview.superview may not give the cell object and hence the indexPath will be nil.

    In that case we should keep finding the superview until we get the cell object.

    Function to get cell object by superview

    func getCellForView(view:UIView) -> UITableViewCell?
    {
        var superView = view.superview
    
        while superView != nil
        {
            if superView is UITableViewCell
            {
                return superView as? UITableViewCell
            }
            else
            {
                superView = superView?.superview
            }
        }
    
        return nil
    }
    

    Now we can get indexPath on button tap as below

    @IBAction func tapButton(_ sender: UIButton)
    {
        let cell = getCellForView(view: sender)
        let indexPath = myTabelView.indexPath(for: cell)
    }
    
    0 讨论(0)
  • 2020-11-21 23:58

    UPDATE: Getting the indexPath of the cell containing the button (both section and row):

    Using Button Position

    Inside of your buttonTapped method, you can grab the button's position, convert it to a coordinate in the tableView, then get the indexPath of the row at that coordinate.

    func buttonTapped(_ sender:AnyObject) {
        let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
        let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
    }
    

    NOTE: Sometimes you can run into an edge case when using the function view.convert(CGPointZero, to:self.tableView) results in finding nil for a row at a point, even though there is a tableView cell there. To fix this, try passing a real coordinate that is slightly offset from the origin, such as:

    let buttonPosition:CGPoint = sender.convert(CGPoint.init(x: 5.0, y: 5.0), to:self.tableView)
    

    Previous Answer: Using Tag Property (only returns row)

    Rather than climbing into the superview trees to grab a pointer to the cell that holds the UIButton, there is a safer, more repeatable technique utilizing the button.tag property mentioned by Antonio above, described in this answer, and shown below:

    In cellForRowAtIndexPath: you set the tag property:

    button.tag = indexPath.row
    button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    

    Then, in the buttonClicked: function, you reference that tag to grab the row of the indexPath where the button is located:

    func buttonClicked(sender:UIButton) {
        let buttonRow = sender.tag
    }
    

    I prefer this method since I've found that swinging in the superview trees can be a risky way to design an app. Also, for objective-C I've used this technique in the past and have been happy with the result.

    0 讨论(0)
  • 2020-11-22 00:02

    In Swift 4 , just use this:

    func buttonTapped(_ sender: UIButton) {
            let buttonPostion = sender.convert(sender.bounds.origin, to: tableView)
    
            if let indexPath = tableView.indexPathForRow(at: buttonPostion) {
                let rowIndex =  indexPath.row
            }
    }
    
    0 讨论(0)
  • 2020-11-22 00:03

    My approach to this sort of problem is to use a delegate protocol between the cell and the tableview. This allows you to keep the button handler in the cell subclass, which enables you to assign the touch up action handler to the prototype cell in Interface Builder, while still keeping the button handler logic in the view controller.

    It also avoids the potentially fragile approach of navigating the view hierarchy or the use of the tag property, which has issues when cells indexes change (as a result of insertion, deletion or reordering)

    CellSubclass.swift

    protocol CellSubclassDelegate: class {
        func buttonTapped(cell: CellSubclass)
    }
    
    class CellSubclass: UITableViewCell {
    
    @IBOutlet var someButton: UIButton!
    
    weak var delegate: CellSubclassDelegate?
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.delegate = nil
    }
    
    @IBAction func someButtonTapped(sender: UIButton) {
        self.delegate?.buttonTapped(self)
    }
    

    ViewController.swift

    class MyViewController: UIViewController, CellSubclassDelegate {
    
        @IBOutlet var tableview: UITableView!
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellSubclass
    
            cell.delegate = self
    
            // Other cell setup
    
        } 
    
        //  MARK: CellSubclassDelegate
    
        func buttonTapped(cell: CellSubclass) {
            guard let indexPath = self.tableView.indexPathForCell(cell) else {
                // Note, this shouldn't happen - how did the user tap on a button that wasn't on screen?
                return
            }
    
            //  Do whatever you need to do with the indexPath
    
            print("Button tapped on row \(indexPath.row)")
        }
    } 
    
    0 讨论(0)
提交回复
热议问题