How can I get indexPath from uiview in 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 strokes in a view in a view class

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

}

How can I get indexPath to find out which cellular network user voted on? I do not have a button, I have a uiview, so I can not use the following:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
+4
source share
4 answers

, cellForRowAtIndexPath:, . init . touchesBegan:withEvent:, .

+1

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

tap

-(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))
}

, .:)

+12

cellforAtIndexPath indexPath.row

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

UItableViewCell

+1

There is a specific delegation method for UITableView ie cellForRowIndexPath :. There is no need to add any Tap Gesture or touchhesBegan: method in your code. TableView already has this. You can get the index path for the selected cell in this cellFor RowIndexPath: method.

0
source

Source: https://habr.com/ru/post/1611733/


All Articles