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 = Int()
func buttonPressed(sender: AnyObject) {
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
println(cellIndexPath)
point = cellIndexPath!.row
println(point)
}
After seeing Paulw11's suggestion of using a delegate callback, I wanted to elaborate on it slightly/put forward another, similar suggestion. Should you not want to use the delegate pattern you can utilise closures in swift as follows:
Your cell class:
Your
cellForRowAtIndexPath
method:I found a very easy and saficiat way to use for manage any cell in tableView and collectionView by using a Model class and this a work as perfectly.
There is indeed a much better way to handle this now. This will work for manage cell and value
here is my output(screenshote) so see this
here is my code
class RNCheckedModel: NSObject {
class InviteCell: UITableViewCell {
class RNInviteVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
In Swift 4 , just use this:
Try using #selector to call the IBaction.In the cellforrowatindexpath
This way you can access the indexpath inside the method editButtonPressed
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
ViewController.swift
I used convertPoint method to get point from tableview and pass this point to indexPathForRowAtPoint method to get indexPath