gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:03:09 24 4
gpt4 key购买 nike

我有一个带有 TableView 的 View Controller 。每个单元格都有一个带有五星级评级系统的自定义 View 。我在 View 类中处理 View 的 touchesBegan

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

}

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

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

最佳答案

做一件事

在索引路径行的单元格内为该 View 提供手势

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

并处理点击手势

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

希望对您有所帮助。 :)

关于ios - 如何获取单元格上 uiview 的 indexPath - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33143468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com