indexPathForRowAtPoint仅为uitableview中的第一个单元格返回nil [英] indexPathForRowAtPoint returns nil only for first cell in a uitableview

查看:100
本文介绍了indexPathForRowAtPoint仅为uitableview中的第一个单元格返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 UITableViewCell 中有一个 UIButton ,它会在按下按钮时删除单元格。为了获取按钮所在的单元格的indexPath,我在按下按钮时调用的方法中调用 indexPathForRowAtPoint :如下所示:

I have a UIButton inside of a custom UITableViewCell that removes the cell when the button is pressed. In order to get the indexPath of the cell the button is in, I'm calling indexPathForRowAtPoint: in a method called when the button is pressed as follows:

-(void)buttonPressed:(UIButton *)sender{
    CGPoint btnPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:btnPosition];

    if (indexPath != nil)
    {
    //remove the cell from the tableview. 
    }
}

如果按任何单元格中的按钮,这样可以正常工作除了第一个单元格,以及表格中是否只有一个单元格。但是,如果有多个单元格并按下第一个单元格中的按钮,则按下按钮并找到btnPosition,但是 [self.tableView indexPathForRowAtPoint:btnPosition]; 返回 nil

This works fine if I press the button in any cell except the first cell, and also when if there is ONLY one cell in the table. However, if there are multiple cells and I press the button in the first cell, the button press is triggered and the btnPosition is found, but [self.tableView indexPathForRowAtPoint:btnPosition]; returns nil.

例如,如果有三个单元格,每个单元格都有一个按钮:

For example, if there are three cells, each with a button:


  1. 我按下第一个单元格按钮: buttonPressed被调用但indexPath为零,所以没有任何反应。

  2. 我按下第三个单元格按钮: buttonPressed被调用,单元格被按预期移除。

  3. 我再次按下第一个单元格:与之前相同,buttonPressed被调用但indexPath为nil。

  4. 我按下第二个单元格按钮: buttonPressed被调用,单元格被按预期删除。

  5. 我按下第一个单元格按钮:这次调用了buttonPressed,indexPath不是nil,并且按预期删除了单元格。

  1. I press the first cell button: buttonPressed is called but indexPath is nil, so nothing happens.
  2. I press the third cell button: buttonPressed is called and the cell is removed as expected.
  3. I press the first cell again: same as before, buttonPressed called but indexPath is nil.
  4. I press the second cell button: buttonPressed is called and the cell is removed as expected.
  5. I press the first cell button: This time, buttonPressed is called, indexPath is not nil and the cell is removed as expected.

我已经检查了标签leView不是零,正如相关但不同的帖子所暗示的那样。我还确认点 btnPosition 设置为相同的值(x = 260,y = 44.009995) [self.tableView indexPathForRowAtPoint:btnPosition] == nil AND时 [self.tableView indexPathForRowAtPoint:btnPosition]!= nil

I've checked that the tableView is not nil, as suggested by a related, but different, post. I've also confirmed that the point btnPosition is set to the same value (x=260, y=44.009995) both when [self.tableView indexPathForRowAtPoint:btnPosition] == nil AND when [self.tableView indexPathForRowAtPoint:btnPosition] != nil.

除了询问是否有人对这里可能发生的事情有任何想法外,我的问题是:

So aside from asking if anyone has any ideas on what could be happening here, my question is:

在什么情况下可以将相同的CGPoint传递给 [self.tableView indexPathForRowAtPoint:btnPosition] 返回不同的(或无) indexPath

Under what circumstances could passing the same CGPoint to [self.tableView indexPathForRowAtPoint:btnPosition] return a different (or nil) indexPath?

我很感激能帮助我解决这个问题,或者听听是否有人遇到过类似的问题。

I'd appreciate help with any ideas of where I might look to track this down, or to hear if anyone has encountered a similar issue.

一些可能有帮助的补充说明(如果我提出问题,请原谅我这样做。我很乐意接受你的反馈意见:)

Some additional notes that might be helpful (please excuse if I make a question asking faux-pas. I'll happily take your feedback about that as well :)


  • 此tableView是 UITableViewController 的一部分,它嵌入在 UINavigationController

  • This tableView is part of a UITableViewController which is embedded in a UINavigationController

tableView有3个部分,其中2个部分是隐藏的(0行,没有页脚,没有标题)而我正在使用所描述的按钮单元格行显示该部分。

The tableView has 3 sections, 2 of which are hidden from view (0 rows, no footer, no header) while I'm presenting the section with the button cell rows as described.

我正在通过编程方式更改水平约束来调整演示期间按钮的位置。

I'm adjusting the location of the buttons during presentation by programmatically changing their horizontal constraints.

我的 customUITableViewCell的高度 tableViewRows UIButton 均等于60.0f

The heights of my customUITableViewCell, tableViewRows and UIButton are each equal to 60.0f

推荐答案

试试这个:

修复目标选择器:

[button addTarget:self action:@selector(buttonPressed:event:)forControlEvents:UIControlEventTouchUpInside];


- (void)buttonPressed:(id)sender event:(id)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPos = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPos];
    if(indexPath != nil){
        //do operation with indexPath
    }
}

这篇关于indexPathForRowAtPoint仅为uitableview中的第一个单元格返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆