1

I add a button to each cell in my UITableView (via Storyboard) and assign it a controlEvent UIControlEventTouchUpInside like so:

UIButton *postToBTN = (UIButton *)[cell viewWithTag:16]; [postToBTN addTarget:self action:@selector(postToFeed:) forControlEvents:UIControlEventTouchUpInside];

Just doing a quick test via NSLog, I see that my second cell is always incorrect.

    - (IBAction)postToFeed:(id)sender {
  CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
  NSLog(@"indexPath.section = %ld",(long)indexPath.section);
}

Given each section has only one row, my print out looks like this:

indexPath.section = 0
indexPath.section = 0
indexPath.section = 1
indexPath.section = 2

No matter how many elements I have in my posts array (will always be section 0 followed by 0 again with everything else immediately after correct)

Any idea as to what might be going on? Any chance this has to do with how I'm calling dequeueResuableCellWithIdentifier given that I'm displaying different prototype cells in the same UITableView?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  //Where we configure the cell in each row
  static NSString *CellIdentifier1 = @"Cell1";
  static NSString *CellIdentifier2 = @"Cell2";
  static NSString *CellIdentifier = @"TBD";

  GenericPost *post = [self.posts objectAtIndex:indexPath.section];
  if ([post.typeOfPost isEqualToString:@"Cell1"]){
    CellIdentifier = CellIdentifier1;
  }

  else if ([post.typeOfPost isEqualToString:@"Cell2"]){
    CellIdentifier = CellIdentifier2;
  }


  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }

UPDATED

Using CGPointZero

2014-04-21 13:00:37.179 Test[67004:60b] *** sender Position = (92.000000,-2.000000)
2014-04-21 13:00:37.179 Test[67004:60b] *** buttonPosition = (107.000000,146.000000)
2014-04-21 13:00:37.180 Test[67004:60b] *** indexPath is NIL!! ***
2014-04-21 13:00:37.180 Test[67004:60b] indexPath.section = 0
2014-04-21 13:00:37.181 Test[67004:60b] PostText = Today was a swell day
2014-04-21 13:00:37.938 Test[67004:60b] *** sender Position = (92.000000,-2.000000)
2014-04-21 13:00:37.939 Test[67004:60b] *** buttonPosition = (107.000000,239.000000)
2014-04-21 13:00:37.939 Test[67004:60b] indexPath.section = 0
2014-04-21 13:00:37.940 Test[67004:60b] PostText = Today was a swell day
2014-04-21 13:00:38.570 Test[67004:60b] *** sender Position = (92.000000,-2.000000)
2014-04-21 13:00:38.571 Test[67004:60b] *** buttonPosition = (107.000000,348.500000)
2014-04-21 13:00:38.571 Test[67004:60b] indexPath.section = 1
2014-04-21 13:00:38.572 Test[67004:60b] PostText = Just trying to get some new socks. Here's some more text to fill up two lines of text

Using CGPointMake(5,5)

2014-04-21 13:02:13.080 Test[67030:60b] *** sender Position = (92.000000,-2.000000)
2014-04-21 13:02:13.080 Test[67030:60b] *** buttonPosition = (112.000000,151.000000)
2014-04-21 13:02:13.081 Test[67030:60b] indexPath.section = 0
2014-04-21 13:02:13.081 Test[67030:60b] PostText = Today was a swell day
2014-04-21 13:02:13.688 Test[67030:60b] *** sender Position = (92.000000,-2.000000)
2014-04-21 13:02:13.688 Test[67030:60b] *** buttonPosition = (112.000000,244.000000)
2014-04-21 13:02:13.689 Test[67030:60b] indexPath.section = 1
2014-04-21 13:02:13.689 Test[67030:60b] PostText = Just trying to get some new socks. Here's some more text to fill up two lines of text
2014-04-21 13:02:14.288 Test[67030:60b] *** sender Position = (92.000000,-2.000000)
2014-04-21 13:02:14.288 Test[67030:60b] *** buttonPosition = (112.000000,353.500000)
2014-04-21 13:02:14.289 Test[67030:60b] indexPath.section = 2
2014-04-21 13:02:14.289 Test[67030:60b] PostText = Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

And:

2014-04-21 13:02:12.214 Test[67030:60b]  ### HeaderView.height = 148.000000 ###
SimplyLearning
  • 269
  • 3
  • 11
  • I'm pretty sure you shouldn't have `CellIdentifier` as `static`! – Rich Apr 21 '14 at 01:48
  • `indexPathForRowAtPoint:` will return nil if the point is out of the bounds of any row. – KudoCC Apr 21 '14 at 02:17
  • you should probably make sure the origin (CGPointZero) of the button falls inside the row. and then check regarding the comment by @KudoCC – riadhluke Apr 21 '14 at 03:41
  • Thanks guys, for leading me in the right direction! Turned out my indexPathForRowAtPoint: was nil for my first cell. Followed a suggested solution solution outlined here: http://stackoverflow.com/questions/22368525/indexpathforrowatpoint-returns-nil-only-for-first-cell-in-a-uitableview. Updated my initial post with my logs, but while I am happy I have a fix, I'm also trying to figure out why changing the reference frame works as I don't seem to have the same rounding issues as the guy in the previous post. With the log printouts, any ideas? – SimplyLearning Apr 21 '14 at 17:07

0 Answers0