convertPoint returning wrong data in tableview

I have a tableview with 4 sections. Each section has a button. I used "convertPoint" to View to determine which button was touched and then get the indexPath from the "point".

With the latest iOS update the point data returned is incorrect and thus the wrong indexPath.row is selected for action.

Replies

convertPoint is not the best way to do this.

You could:

  • set a tag for each button, equal to the indexPath.row of its cell
  • then when you tap, you get immediately the row

Note: as you have sections (which will not have more than 1000 rows each I assume),

  • set the tag as a multiplex of section and row, such as 1000 * indexPath.section + indexPath.row
  • demultiplex when you get the tag with:
let row = tag % 1000
let section = (tag - row) / 1000

or also

  • find the cell in which the button is.
  • in the IBAction
        var view = sender.superview   // the button superview
        while (view != nil && view!.isKind(of: UITableViewCell.self) == false) { // We stop when no more superview or found a cell. Then view contains the cell
            view = view!.superview
        }
        if let cell = view as? UITableViewCell {  // Or the type of your custom cell
          // Do what you need with cell
       }

Thanks but this code has been in the app for 7+ years and has worked fine until a recent iOS update.

  • If you don't show code, it is impossible to say. Which version of iOS ? And tell precisely what value you get, what value you expected.

Add a Comment
  • (IBAction)connectButtonTouched:(id)sender {

    BOOL typeOFAction;     // Determine which Boat Connect Button was touched.     CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.boatsAndDevicesTable];     NSIndexPath *indexPath = [self.boatsAndDevicesTable indexPathForRowAtPoint:buttonPosition]          NSLog(@"sender - %@", sender);     NSLog(@"sender - %@", ((UIButton *)sender).layer);     NSLog(@"button position - %f - %f", buttonPosition.x, buttonPosition.y);     NSLog(@"Index Path Section - %ld", (long)indexPath.section);

    BoatWithStatus *boatForConnectButton = [self.boatsWithStatus objectAtIndex:indexPath.section-1];

The value returned from "convertPoint toView:" is not consistent. Sometimes it matches the section/row and sometimes it doesn't.

I have tried different button actions (touch up, touch down) but the results are the same.

I reformatted to get it more readable.

(IBAction)connectButtonTouched:(id)sender {
   BOOL typeOFAction;  // Determine which Boat Connect Button was touched.  
   CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.boatsAndDevicesTable];  
   NSIndexPath *indexPath = [self.boatsAndDevicesTable indexPathForRowAtPoint:buttonPosition]     

   NSLog(@"sender - %@", sender);  
   NSLog(@"sender - %@", ((UIButton *)sender).layer);  
   NSLog(@"button position - %f - %f", buttonPosition.x, buttonPosition.y);     
   NSLog(@"Index Path Section - %ld", (long)indexPath.section);

  BoatWithStatus *boatForConnectButton = [self.boatsWithStatus objectAtIndex:indexPath.section-1];

I tested similar code in Swift (but with a single section)

    @IBAction func testInCell(_ sender: UIButton) {

        let buttonPosition = sender.convert(CGPoint.zero, to: self.tableForItem2)
        let indexPath = self.tableForItem2.indexPathForRow(at: buttonPosition)
        print(buttonPosition, indexPath)
    }

Tested on simulators iOS 15.2 and 15.4 (Xcode 13.2.1 and Xcode 13.3).

It works correctly.

In your case, is section wrong or both section and row ?

Could you give an example (NSLogs) of wrong results (and print as well row).

Here are the logs.... Also note that is requires at least 4 sections before the problem exists.

It his case, the button touched was in Section 1.

2022-04-28 17:08:27.113602-0400 CMonster[19263:907393] sender - <UIButton: 0x149e44370; frame = (300 10; 100 40); clipsToBounds = YES; opaque = NO; autoresize = LM+BM; layer = <CALayer: 0x2836fa8c0>> 2022-04-28 17:08:27.114013-0400 CMonster[19263:907393] sender - <CALayer: 0x2836fa8c0> 2022-04-28 17:08:27.114162-0400 CMonster[19263:907393] button position - 300.000000 - 190.000000 2022-04-28 17:08:27.114301-0400 CMonster[19263:907393] Index Path Section - 4 2022-04-28 17:08:27.114430-0400 CMonster[19263:907393] Index Path Row - 0

Returned to the same screen and touched the same button in Section 1.

2022-04-28 17:10:06.797661-0400 CMonster[19263:907393] sender - <UIButton: 0x149e57850; frame = (300 10; 100 40); clipsToBounds = YES; opaque = NO; autoresize = LM+BM; layer = <CALayer: 0x2836f5280>> 2022-04-28 17:10:06.797981-0400 CMonster[19263:907393] sender - <CALayer: 0x2836f5280> 2022-04-28 17:10:06.798119-0400 CMonster[19263:907393] button position - 300.000000 - 10.000000 2022-04-28 17:10:06.798264-0400 CMonster[19263:907393] Index Path Section - 1 2022-04-28 17:10:06.798407-0400 CMonster[19263:907393] Index Path Row - 0

Correction. There are no issues when there are 2 sections. The problems start when there are 3 or more sections.

  • Updated to the latest Xcode and the problem is the same.

Add a Comment