CGRect“不包含点”目标-c

时间:2013-06-15 10:27:04

标签: iphone uitableview

我在RootViewController中有一个UITableView,当点击一个单元格时,我希望它有机会颜色,但一次只能启用一个。

我很难说“如果CGRect不包含水龙头”。

.m

- (void)changeColour:(UITapGestureRecognizer *)tap
{
    if (UIGestureRecognizerStateEnded == tap.state) {
        UITableView *tableView = (UITableView *)tap.view;
        CGPoint p = [tap locationInView:tap.view];
        NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        CGPoint pointInCell = [tap locationInView:cell];
        if (CGRectContainsPoint(cell.frame, p)) {
            UIView* view1 = [[UIView alloc] init];
                       view1.backgroundColor = [UIColor redColor];
                       [cell setBackgroundView:view1];

        } else  {
                       UIView* view1 = [[UIView alloc] init];
                       view1.backgroundColor = [UIColor whiteColor];
                       [cell setBackgroundView:view1];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

更好的方法是不要使用手势......

使用TableView Delegate方法......

要使用此功能,请确保已将委托分配给您的tableview ..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIView* view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor redColor];
    [cell setBackgroundView:view1];
    [view1 release];//for NON ARC ONLY
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIView* view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor whiteColor];
    [cell setBackgroundView:view1];
    [view1 release];//for NON ARC ONLY
}

这肯定会对你有所帮助

享受编码........