简体   繁体   English

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

[英]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. 出于某些奇怪的原因,当用户在UITableView中点击一个单元格时, cell.backgroundView图像将清除,并且仅显示我设置为占位符的灰色背景。 As far as I can tell, there's nothing in didSelectRowAtIndexPath that would tell the backgroundView to reset. 据我所知, didSelectRowAtIndexPath中没有任何东西可以告诉backgroundView重置。 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];
    }
In cellForRowAtIndexPath write this line.
This may help u

cell.selectionStyle = UITableViewCellSelectionStyleNone;

inside didSelectRowAtIndexPath method Store index and Reload table view then inside cellForRowAtIndexPath method check 在didSelectRowAtIndexPath方法内部存储索引并重新加载表视图,然后在cellForRowAtIndexPath方法内部进行检查

if (indexPath.row == storedIndex) { 如果(indexPath.row == storedIndex){

    cell.backgroundColor = [UIColor clearColor];

} else { }其他{

    cell.backgroundColor = [UIColor whiteColor];

} }

You need to set tableview selection styleNone from your customeCell or by Coding. 您需要从customeCell或通过编码设置tableview选择styleNone。

  • go to your cell > Attributes Inspector > Selection > None. 转到您的单元>属性检查器>选择>无。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM