Andrea F
Andrea F

Reputation: 733

CGRect "does not contain point" objective-c

I have a UITableView within a RootViewController and when a cell is tapped I want it to chance colour but only one should be enabled at one time.

I'm having difficulty trying to say "if CGRect does not contain tap".

.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];
        }
    }
}

Upvotes: 1

Views: 269

Answers (1)

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

Better way is to not to use gestures...

Use TableView Delegate methods...

To use this make sure you have assign delegates to your 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
}

This will definitely help you

Enjoy coding........

Upvotes: 1

Related Questions