DanceSC
DanceSC

Reputation: 521

UITableViewCell background color on click is turning white

I have a UITableViewCell that is populated with an array pulled from a PHP page. Based off of the screen orientation the cell may be wide enough to fit two images instead of just one. So I detect the screen orientation, and then add in two image views (One that takes up the left half of the screen and consists of all the odd index numbers, and one that takes up the right half of the screen and consists of all the even index numbers). If I have an odd number of images, then the very last one is left blank.

The problem is that when I have an odd number of images, and I click the very last cell (the cell that would hold two, but instead only holds one), it changes the background color to white / a very light grey. How can I change this so that clicking the cell does not change the background color?

Note the below code is just a snippet. I left out the screen width detection and orientation to keep everything relevant.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIndentifier = @"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];


CGRect result = [UIScreen mainScreen].bounds;
CGFloat width = CGRectGetWidth(result);
CGFloat height = CGRectGetHeight(result);

UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
    result.size = CGSizeMake(width, height);
} else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    result.size = CGSizeMake(height, width);
}

UIImageView *myImageView;
UIImageView *myImageView2;

myCell.contentView.backgroundColor = [ UIColor colorWithRed:0/255.0f green: 17/255.0f blue:34/255.0f alpha:1];

if(result.size.width <= 480) {
    Companies *item = _feedItems[indexPath.row];

    myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.listTableView.bounds), (item.imageName.size.height / item.imageName.size.width) * CGRectGetWidth(self.listTableView.bounds))];

    myImageView.tag = indexPath.row;
    myImageView.image = item.imageName;
    myImageView.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTap:)];
    [tap setNumberOfTouchesRequired:1];
    [tap setNumberOfTapsRequired:1];
    //[tap setDelegate:self];

    [myImageView addGestureRecognizer:tap];

    [myCell addSubview:myImageView];
}
if(result.size.width > 480) {
    NSLog(@"IndexPath.row: %d, feedItems: %d",indexPath.row, _feedItems.count);
    if((indexPath.row*2+1) <= _feedItems.count) {
        Companies *item = _feedItems[indexPath.row * 2];
        myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth(self.listTableView.bounds) / 2, (item.imageName.size.height / item.imageName.size.width) / 2 * CGRectGetWidth(self.listTableView.bounds))];
        myImageView.tag = indexPath.row*2;
        myImageView.image = item.imageName;
        myImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTap:)];
        [tap setNumberOfTouchesRequired:1];
        [tap setNumberOfTapsRequired:1];
        [myImageView addGestureRecognizer:tap];
        [myCell addSubview:myImageView];

        if ((indexPath.row*2+1) < _feedItems.count) {
            Companies *item2 = _feedItems[(indexPath.row * 2) + 1];
            myImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.listTableView.bounds) / 2,0,CGRectGetWidth(self.listTableView.bounds) / 2, (item.imageName.size.height / item.imageName.size.width) / 2 * CGRectGetWidth(self.listTableView.bounds))];
            myImageView2.tag = indexPath.row*2+1;
            myImageView2.image = item2.imageName;
            myImageView2.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTap:)];
            [tap2 setNumberOfTouchesRequired:1];
            [tap2 setNumberOfTapsRequired:1];
            [myImageView2 addGestureRecognizer:tap2];
            [myCell addSubview:myImageView2];
        } else {
            //myCell.contentView.backgroundColor = [ UIColor colorWithRed:0/255.0f green: 17/255.0f blue:34/255.0f alpha:1];
        }
    }
}

return myCell;
}

This only happens when the second Image does not exist and the user clicks in that empty space. Does anyone know what I am missing, or what is causing the field to go back to white? In the Attributes Inspector I set the Selection to None hoping that would do something.

Upvotes: 1

Views: 1054

Answers (1)

Karan Pal
Karan Pal

Reputation: 77

In the tableview delegate method cellforrowatindexpath try this

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

Upvotes: 3

Related Questions