团队,
我有 UITableView,其中包含 50 多个单元格。 每个单元格的宽度为60。
当我滚动到 20 个单元格时,然后点击任何单元格
它给出单元格值上方的索引路径值而不是单击单元格值
在 CellForRowAtIndexPath 内部
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell checkButton] addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)checkButtonAction:(id)sender
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
}
如果选择的索引是 21,则给出索引 20,保持索引计数从零开始。
这个问题是在 iOS 10.1 iphone 7 plus 设备中观察到的,不是在模拟器 iPhone 7 plus iOS 10.1 (14B72) 中
调试值 {长度 = 2,路径 = 0 - 14}
NSIndexPath 路径应该是 0 - 15 但它给出了 0 - 14。
最佳答案
[YOURBUTTON addTarget:self action:@selector(METHODNAME:event:) forControlEvents:UIControlEventTouchDown];
按钮点击方法
-(IBAction)METHODNAME:(UIButton*)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:YOUR_TABLEVIEW];
NSIndexPath *indexPath = [YOUR_TABLEVIEW indexPathForItemAtPoint: currentTouchPosition];
// your requirement code
}
如果对此代码有任何疑问,请将您的评论放在答案中。 祝你好运
快乐编码。
关于ios - UITableView selected indexpath 值给错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41217716/