[英]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.