如何防止UITableViewCell的backgroundView在点击时清除?

时间:2016-01-28 22:24:14

标签: ios objective-c uitableview

由于一些奇怪的原因,当用户点击我的UITableView中的单元格时,cell.backgroundView图像会清除,它只会显示我设置为占位符的灰色背景。据我所知,didSelectRowAtIndexPath中没有任何内容可以告诉backgroundView重置。为什么会这样?

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

3 个答案:

答案 0 :(得分:1)

In cellForRowAtIndexPath write this line.
This may help u

cell.selectionStyle = UITableViewCellSelectionStyleNone;

答案 1 :(得分:0)

在didSelectRowAtIndexPath方法中存储索引和重载表视图然后在cellForRowAtIndexPath方法中检查

if(indexPath.row == storedIndex){

    cell.backgroundColor = [UIColor clearColor];

} 别的{

    cell.backgroundColor = [UIColor whiteColor];

}

答案 2 :(得分:0)

您需要从您的customeCell或Coding设置tableview选择styleNone。

  • 转到您的手机>属性检查器>选择>无。