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 ###