也许这个问题还不够清楚。
我的意思是,我有一个TableView,它有一个原型细胞,这个细胞有一个按钮。
我设置了一个名为"ForMore“的segue名称,它是组合2 ViewControllers
但我多次重复使用这个细胞。
这就是结果:
当我单击所有cells.It中的每个按钮时,都会跳转到另一个ViewController。
但是我需要知道我单击了哪个按钮,因为ViewController需要根据我单击的单元格进行初始化。
所以,当我多次重复使用一个单元时,我怎么知道我单击了哪个单元格。
发布于 2015-07-27 08:47:17
使用自定义单元格进行任何操作的最佳方法是使用自定义类。
这样,按钮是类的属性,可以将操作发送到单元格。
这样,您就可以使用块来自定义按钮在创建按钮时所做的事情。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// create the cell...
cell.buttonBlock = {
self.buttonTappedAtIndexPath(indexPath)
}
return cell
}或者类似这样的东西。
发布于 2015-07-27 08:54:34
我也有过这样的问题,我也是这样做的。
// 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
}发布于 2015-07-28 01:16:56
我已经尝试了以上所有的方法,但没有work.Maybe,我不能很好地理解答案。
但我自己解决了这个问题。
我就是这样做的:
1创建一个新的类继承名为‘UIButton’的ForMore,并声明一个名为‘row’的变量
2 ForMoreButton将ForMoreButton组合到自定义单元格,名称为“ForMoreButton”
当我在方法中创建单元格时,将indexPath.row分配给cell.ForMoreButton.row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell4 ViewController my中的4、,将ForMoreButton直接与动作‘ButtonTapped’组合到此ViewController,然后我们就可以从(发件人为!( ForMore).row
https://stackoverflow.com/questions/31648451
复制相似问题