我在UITableView的每个单元格中添加了一个按钮(通过Storyboard)并为其分配了一个controlEvent UIControlEventTouchUpInside
,如下所示:
UIButton *postToBTN = (UIButton *)[cell viewWithTag:16];
[postToBTN addTarget:self action:@selector(postToFeed:) forControlEvents:UIControlEventTouchUpInside];
只是通过NSLog
进行快速测试,我发现我的第二个单元格始终不正确。
- (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);
}
鉴于每个部分只有一行,我的打印输出如下:
indexPath.section = 0
indexPath.section = 0
indexPath.section = 1
indexPath.section = 2
无论我的posts
数组中有多少元素(将始终为0部分,然后再次为0,其他所有内容在正确之后立即执行)
知道可能发生的事情吗?如果我在同一个UITableView中显示不同的原型单元,那么这与我调用dequeueResuableCellWithIdentifier
的方式有什么关系吗?
- (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];
}
已更新
使用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
使用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.
和
2014-04-21 13:02:12.214 Test[67030:60b] ### HeaderView.height = 148.000000 ###