简体   繁体   English

当我按下按钮推送到另一个ViewController时,如何知道哪个indexPath.row我的按钮?

[英]How can I know which indexPath.row my button in when I press a button to push to another ViewController?

Maybe the question is not clearly enough. 也许问题不够清楚。

I mean I have a TableView,and it has a Prototype Cell and this Cell has a button. 我的意思是我有一个TableView,它有一个Prototype Cell,这个Cell有一个按钮。

I set a segue name "ForMore" which is combine 2 ViewControllers 我设置了一个名为“ForMore”的segue名称,它组合了2个ViewControllers

But I reuse this Cell many times. 但我多次重复使用这个Cell。

And this is result: 这是结果:

When I click everyone of the button in all cells.It will jump to another ViewController. 当我单击所有单元格中的每个按钮时,它将跳转到另一个ViewController。

But I need to know which button I have clicked because that ViewController need initialize according to which cell I clicked. 但我需要知道我点击了哪个按钮,因为ViewController需要根据我点击的单元格进行初始化。

So, how can I know which cell I clicked when I reuse a cell many times. 那么,当我多次重复使用单元格时,如何知道我单击了哪个单元格。

The best way of doing anything with custom cells is to have a custom class to go along with it. 使用自定义单元格执行任何操作的最佳方法是使用自定义类。

That way the button is a property of the class and the action can be sent to the cell instead. 这样,按钮是类的属性,操作可以发送到单元格。

That way you can use a block to customise what the button does at the point of creating it. 这样,您可以使用块来自定义按钮在创建时所执行的操作。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // create the cell...

    cell.buttonBlock = {
        self.buttonTappedAtIndexPath(indexPath)
    }

    return cell
}

Or something like this. 或类似的东西。

I also had this problem once, and I did this way. 我曾经遇到过这个问题,我这样做了。

// CustomCellClass

var tableViewdelegate: MyTableViewDelegateClass?

// button in cell action
@IBAction func buttonPressed(sender: AnyObject) {
    tableViewdelegate?.buttonPressedInCell(cell: self)
}

// MyTableViewDelegateClass

func buttonPressedInCell(cell: CustomCellClass) {
    let indexPath = tableView.indexPathForCell(cell)

    // other logic you have
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell =  // create your cell or deque

    cell.tableViewdelegate = self

    return cell
}

I have tried all of the method above but didn't work.Maybe I can't understand the answers well. 我已经尝试了上述所有方法,但没有奏效。也许我无法理解答案。

But I fixed this problem by my self. 但我自己解决了这个问题。

This is what I did: 这就是我做的:

1、Create a new class inherit UIButton named 'ForMore' and declare a var named 'row' 1,创建一个新类继承名为'ForMore'的UIButton并声明一个名为'row'的var

2、Combine the ForMoreButton to the custom cell with a name 'ForMoreButton' 2,将ForMoreButton与名为'ForMoreButton'的自定义单元格相结合

3、Assign the indexPath.row to cell.ForMoreButton.row when I create the cell in the method 3,当我在方法中创建单元格时,将indexPath.row分配给cell.ForMoreButton.row

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

4、In the ViewController my tableview inside, combine the ForMoreButton to this ViewController directly with an action 'ButtonTapped' and then we can get the row from (sender as! ForMore).row 4,在ViewController中我的tableview里面,直接将ForMoreButton与这个ViewController结合使用'ButtonTapped'动作,然后我们可以从(发送者为!ForMore).row获取行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击表格视图单元格上的按钮时如何获取indexPath.row? - How to get the indexPath.row when I click a button on a table view cell? 如何知道在 IBAction 上单击了哪个 IndexPath.row,以及如何从触发中排除特定按钮 - How to know which IndexPath.row is clicked on IBAction, and how to exclude a specific button from it being triggered 在一个按钮上的IndexPath.row? - IndexPath.row on a button? 当知道iOS中uitableview的indexPath.row时如何获取哪个uibutton - how can get which uibutton when know indexPath.row of uitableview in ios 当我在单元格上点击按钮时尝试获取 indexpath.row 时,编译器抱怨单元格为零 - Compiler is complaining that cell is nil when I try to get indexpath.row when button is tapped on cell 如何将indexPath(而不是indexPath.row)传递到UITableViewCell的IBAction中 - How do I pass indexPath (not indexPath.row) into my IBAction for my UITableViewCell 如何通过 indexPath.row 或仅行号而不是 indexpath 获得 uitableViewCell? - How can I get a uitableViewCell by indexPath.row or only row number instead of indexpath? 如何在CustomCell按钮中检测indexPath.row单击? - How to detect indexPath.row in CustomCell button Click? 使用indexPath.row保存收藏夹按钮状态 - Save Favorite button state with indexPath.row 如何将indexPath.row保存为全局整数? - How do I save indexPath.row as global integer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM