UITableView选择的indexpath值给出错误

kir*_*ran 3 objective-c uitableview didselectrowatindexpath nsindexpath ios

球队,

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

 }
Run Code Online (Sandbox Code Playgroud)

如果选择的索引是21给出索引20,则保持索引计数从零开始.

这个问题在iOS 10.1 iphone 7 plus设备中观察到,不在模拟器iPhone 7加iOS 10.1(14B72)中

调试值{length = 2,path = 0 - 14}

NSIndexPath路径应为0 - 15但它给出0 - 14.

Him*_*iya 5

[YOURBUTTON addTarget:self action:@selector(METHODNAME:event:) forControlEvents:UIControlEventTouchDown];
Run Code Online (Sandbox Code Playgroud)

按钮单击方法

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

如果有任何关于此代码的查询,请将您的评论置于答案中.祝好运

快乐的编码.