如何在单元格上获取uiview的indexPath - iOS

Kat*_*ina 4 objective-c uitableview ios

我有一个带有表视图的视图控制器.每个单元格都有一个带五星评级系统的自定义视图.我在视图的类中处理视图的touchesBegan

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

}
Run Code Online (Sandbox Code Playgroud)

如何获取indexPath以了解哪个单元用户投票?我没有按钮我有uiview所以我不能使用以下内容:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
Run Code Online (Sandbox Code Playgroud)

iDe*_*per 10

做一件事

在索引路径的行中为单元格内的视图提供手势

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

并处理轻拍手势

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

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!)
}
Run Code Online (Sandbox Code Playgroud)

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))
}
Run Code Online (Sandbox Code Playgroud)

希望这对你有所帮助.:)