Andrew
Andrew

Reputation: 3989

How to prevent UITableViewCell's backgroundView from clearing when tapped?

For some strange reason, when a user taps on a cell in my UITableView, the cell.backgroundView image clears and it just displays the gray background that I set as the placeholder. As far as I can tell, there's nothing in didSelectRowAtIndexPath that would tell the backgroundView to reset. Why is this happening?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            // Cell Identifier
            static NSString *SecondCellIdentifier = @"SecondMatchCenterCell";
            MatchCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];
            if (!cell) {
                // if no cell could be dequeued create a new one
                cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SecondCellIdentifier];
            }

            // Separator style
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

            // title of the item
            _searchTerm = [[[[_matchCenterData  objectAtIndex:indexPath.section] objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Search Term"];
            cell.textLabel.text = _searchTerm;

            // Display placeholder while downloading images using background thread
            cell.backgroundColor = [UIColor lightGrayColor];
            cell.imageView.image = nil;

            // asynchronously download image
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

                NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterData[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];

                dispatch_async(dispatch_get_main_queue(), ^{
                    // Setup imageView
                    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imageData]];
                    imageView.contentMode = UIViewContentModeScaleAspectFill;
                    imageView.clipsToBounds = YES;

                    // create effect
                    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

                    // add effect to an effect view
                    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
                    effectView.frame = self.view.frame;

                    // add the effect view to the image view
                    [imageView addSubview:effectView];
                    cell.backgroundView = imageView;
                });

            });

            return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        _searchTerm = [[[[_matchCenterData  objectAtIndex:indexPath.section] objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Search Term"];
        NSLog(@"The search term that was just selected: %@", _searchTerm);
        //Set _sectionSelected variable to the section index
        self.sectionSelected = indexPath.section;
        self.sectionSelectedSearchTerm = _searchTerm;

    [self performSegueWithIdentifier:@"MCExpandedSegue" sender:self];
    }

Upvotes: 0

Views: 61

Answers (3)

Vvk
Vvk

Reputation: 4043

You need to set tableview selection styleNone from your customeCell or by Coding.

  • go to your cell > Attributes Inspector > Selection > None.

Upvotes: 0

Hasmukh Patel
Hasmukh Patel

Reputation: 1

inside didSelectRowAtIndexPath method Store index and Reload table view then inside cellForRowAtIndexPath method check

if (indexPath.row == storedIndex) {

    cell.backgroundColor = [UIColor clearColor];

} else {

    cell.backgroundColor = [UIColor whiteColor];

}

Upvotes: 0

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

In cellForRowAtIndexPath write this line.
This may help u

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Upvotes: 1

Related Questions