Katerina
Katerina

Reputation: 187

How can I get indexPath of a uiview on a cell - iOS

I have a view controller with a table view. Each cell has a custom view with a five star rating system. I handle touchesBegan of the view at the class of the view

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    [self handleTouchAtLocation:touchLocation];

}

How can I get the indexPath in order to know on which cell user voted? I don't have a button I have a uiview so I cannot use the following:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

Upvotes: 4

Views: 3434

Answers (4)

iDeveloper
iDeveloper

Reputation: 627

Do one thing

Give gesture to that view inside cell for row at index path

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOnView:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[viewnanme addGestureRecognizer:singleTap];

and to handle tap gesture

-(void)tappedOnView:(UITapGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:tableView];
NSIndexPath *ipath = [tableView indexPathForRowAtPoint:location];
UITableViewCell *cellindex  = [tableView cellForRowAtIndexPath: ipath];
}

Swift 3+

var singleTap = UITapGestureRecognizer(target: self, action: #selector(self.tappedOnView))
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
viewnanme.addGestureRecognizer(singleTap)


func tapped(onView gesture: UITapGestureRecognizer) {
    let location: CGPoint = gesture.location(in: tableView)
    let ipath: IndexPath? = tableView.indexPathForRow(at: location)
    let cellindex: UITableViewCell? = tableView.cellForRow(at: ipath!)
}

Swift 4+

var singleTap = UITapGestureRecognizer(target: self, action: 
#selector(self.tappedOnView))
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
viewnanme.addGestureRecognizer(singleTap)

func tapped(onView gesture: UITapGestureRecognizer) 
{
    let location: CGPoint = gesture.location(in: tableView)
    let ipath: IndexPath? = tableView.indexPathForRow(at: location)
    let cellindex: UITableViewCell? = tableView.cellForRow(at: ipath ?? 
 IndexPath(row: 0, section: 0))
}

Hope this will help to you. :)

Upvotes: 10

Sakshi
Sakshi

Reputation: 1040

In cellforAtIndexPath keep view's tag as indexPath.row and in below method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

fetch UItableViewCell according to that view tag

Upvotes: 2

Nirzar Gandhi
Nirzar Gandhi

Reputation: 79

There is a given delegate method of UITableView i.e cellForRowIndexPath:. There is no need to add any Tap Gesture or touchesBegan: method in your code. TableView already has this. You can get index path for the selected cell in this method cellFor RowIndexPath:.

Upvotes: 0

Daniel Zhang
Daniel Zhang

Reputation: 5858

You can store your index path in your custom view when you create the view in cellForRowAtIndexPath: by making a property in the class of the view. The property can be set in your init for the custom view. The index path will then be available in your touchesBegan:withEvent: method by reading the property.

Upvotes: 1

Related Questions