ios - UITableView selected indexpath 值给错

标签 ios objective-c uitableview didselectrowatindexpath nsindexpath

团队,

我有 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/

相关文章:

objective-c - Cocoa Touch - 自定义 UIButton 形状

ios - 带有 Parse 的 IOS 中的后台线程

ios - FIRInstanceID 在 FIRInstanceIDAPNSTokenType.Sandbox 中为零

iphone - 无法将 UITableView 的样式更改为在 Interface Builder 中分组

ios - Xcode 自动布局约束困惑

ios - 如何检查 UILabel 是否为空并将内容附加到标签?

objective-c - NSURLCredential 和 NSURLConnection

iphone - 使用 iPhone sdk 以编程方式在 Facebook 墙上发布复杂消息

ios - 从 TableView 中删除行时崩溃

iphone - UITableViewController 中的静态单元格在同一 Nib 上打开不同的 URL