ios - 如果 UITableViewCell 包含 cgrect,则以编程方式选择它

标签 ios uitableview

我有一个 UITableView。我在桌面 View 旁边有一个像箭头一样的图像。

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

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

我已经编写了这段代码,但不起作用:

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 here enter image description here

最佳答案

好吧...对于箭头的问题...我认为表格正在返回每行的矩形,以考虑表格的框架。所以我认为你需要这样做:

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

关于ios - 如果 UITableViewCell 包含 cgrect,则以编程方式选择它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109965/

相关文章:

ios - 如何获得黑色光泽的 UIButton 外观?

ios - 将 UIImageView 添加到自定义 UIButton 类

xcode - 没有名为 dequeueReusableCellWithIdentifier 的成员

iphone - 增加对 scrollViewDidScrollToTop 的计数

ios - 在 iOS 中调整图像大小 — 无法诊断的泄漏

ios - 带有 UITableView 单元格的 Swift 4 AlamofireImage

objective-c - UITableView 数据源问题。当 UITableView :cellForRowAtIndexPath: is called 时,我的数据数组突然为空

ios - 取消选中 Swift4 中的 TableView 所有 TableView 选择

ios - 找出单击单元格的坐标

ios - NSMutablearray 从最后一个元素而不是第一个开始打印