ios - 如何防止 UITableViewCell 的背景 View 在点击时被清除?

标签 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];
    }

最佳答案

In cellForRowAtIndexPath write this line.
This may help u

cell.selectionStyle = UITableViewCellSelectionStyleNone;

关于ios - 如何防止 UITableViewCell 的背景 View 在点击时被清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35073307/

相关文章:

iphone - 测量 UITableView 滚动性能 - iphone

ios - 以编程方式使用自动布局实现带有 subview 的 uiview 子类

ios - 如何摆脱表格单元格的选择样式,但仍然接收 setSelected/设置突出显示的单元格

ios - 区分收到的通知和触摸的通知

objective-c - AVAudioPlayer Number Of Loops 只播放一遍后生效

iphone - 在 UITableViewCell 中动态分配 UIButton 事件

ios - 当我将图像添加到项目时,是否还需要添加 @2x 版本?

android - 基于相同的代码为 iOS、Android 和 WP7 创建库

objective-c - UITableView:从空白部分隐藏标题

ios - Swift:将 UISegmentedControl/UITextField 添加到动态 UITableView