2013-02-27 74 views
0

我有一个UITableView。我在tableview旁边有一个像箭头一样的图像。以编程方式选择一个UITableViewCell,如果它包含cgrect

我必须检查箭头是否与特定单元格相交,以便我可以以编程方式选择该单元格。我该怎么做呢?

我没有在单元格/行的选择类型有问题,但有问题检测箭头是否在特定的行上。

我写了这个代码,但不工作:

NSArray *paths = [tableView indexPathsForVisibleRows]; 

for (NSIndexPath *path in paths) 
{ 
    CGRect myRect = [tableView rectForRowAtIndexPath:indexPath]; 
    CGRect myRect1 = CGRectMake(-12, 234, 55, 52); 

    if (CGRectIntersectsRect(myRect1, myRect)) 
    { 
     // Also tried [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone]; 
     cell.selected = YES; 
     NSLog(@"\n \n \n myrect = %@ myrect1 = %@ , index = %@ \n \n ", NSStringFromCGRect(myRect),NSStringFromCGRect(myRect1), indexPath); 
    } 
    else 
    { 
     cell.selected = NO; 
    } 
} 

图片显示我想要实现,而不是什么什么存在的。

enter image description hereenter image description here

+0

尝试调用“didSelectRowAtIndexPath方法:”编程。 – George 2013-02-27 10:44:48

+0

做了..不起作用... – 2013-02-27 10:47:38

+0

可以请你显示你在'didSelectRowAtIndexPath'中的代码吗?并且在那里放置一个断点以确保它被调用。 – George 2013-02-27 10:51:32

回答

0

我已经找到了答案..发布代码

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView 
{ 

     NSArray *paths = [tableView indexPathsForVisibleRows]; 

     for (NSIndexPath *indexPath in paths) 
     { 

      CGRect myRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]]; 
      CGRect myRect1 = CGRectMake(-12, 236, 20, 30); 

      if (CGRectIntersectsRect(myRect1, myRect) && notSelected == FALSE) 
      { 

       notSelected = TRUE; 
       [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

      } 
      else 
      { 

       notSelected = FALSE; 
       [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
      } 
     } 
} 
1

好吧......为带箭头的问题...我认为表是关于表的框架返回每一行的矩形。因此,我认为你需要做的是:

for (NSIndexPath *path in paths) 
{ 

    CGRect myRect = [tableView rectForRowAtIndexPath:indexPath]; 
    CGRect myRect1 = CGRectMake(-12, 234, 55, 52); 

    myRect.origin.x += tableView.frame.origin.x; 
    myRect.origin.y += tableView.frame.origin.y; 

    if (CGRectIntersectsRect(myRect1, myRect)) 
    { 
     //selection code here 
    } 
    else 
    { 
     // whatever you want 
    } 
} 
+0

感谢乔治..我想我已经找到了答案..将记住,但如果有任何问题发生.. – 2013-02-27 11:25:41

相关问题