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

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

提交回复
热议问题