ios - 带有 UIBlurEffect 的表格 View 变得迟钝

标签 ios objective-c iphone uitableview

我的 tableview 有问题,在使用 uiblurview 实现顶部栏后,tableview 在 iphone5 上是滞后的,滚动 tableview 在添加模糊 View 之前占用 20-25% 的 cpu。工作完美。 期待任何性能帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell%li%i",(long)indexPath.row,cellvar]];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Cell%li%i",(long)indexPath.row,cellvar]];

        if ([self.obrazki count]>0) {
            CGRect imageFrame = CGRectMake((cell.frame.size.width/tableViewOffset)-(tableView.frame.size.width*0.92/2), 10, tableView.frame.size.width*0.92, tableView.frame.size.width*0.92);
            UIImageView * image = [[UIImageView alloc] initWithFrame:imageFrame];
            image.layer.masksToBounds = YES;
            image.layer.cornerRadius = 25;
            [image setImage:[self.obrazki objectAtIndex:indexPath.row]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            image.backgroundColor = [UIColor clearColor];
            [cell addSubview:image];
            cell.backgroundColor = [UIColor clearColor];

            UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
            UIVisualEffectView *visualEffectView= [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            visualEffectView.frame = CGRectMake((cell.frame.size.width/tableViewOffset)-(tableView.frame.size.width*0.92/2)-0.5, 9,tableView.frame.size.width*0.92+1,35);
            CAShapeLayer * maskLayer = [CAShapeLayer layer];
            maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: visualEffectView.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){25.0, 25.0}].CGPath;

            visualEffectView.layer.mask = maskLayer;
            visualEffectView.layer.masksToBounds = YES;
            [cell addSubview:visualEffectView];

            UILabel *likeCount = [[UILabel alloc] initWithFrame:CGRectMake((cell.frame.size.width/tableViewOffset)-(tableView.frame.size.width*0.92/2)+distanceFromStart, 4,tableView.frame.size.width*0.92,35)];
            likeCount.text = [NSString stringWithFormat:@"%@",[imagesLikes objectAtIndex:indexPath.row]];
            likeCount.backgroundColor = [UIColor clearColor];
            likeCount.tintColor = [UIColor blackColor];
            likeCount.font = [UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:20];
            [visualEffectView addSubview:likeCount];
            CGRect imageLikeFrame = CGRectMake(likeCount.frame.origin.x-20, likeCount.frame.origin.y+7.5, 16, 16);
            UIImageView * imageLike = [[UIImageView alloc] initWithFrame:imageLikeFrame];
            [imageLike setImage:[UIImage imageNamed:@"like.png"]];
            imageLike.layer.masksToBounds=YES;
            imageLike.backgroundColor = [UIColor clearColor];
            [visualEffectView addSubview:imageLike];
            NSLog(@"Generating:%i",indexPath.row);
        }
    }
  //  NSLog(@"LoadingOnView:%i",indexPath.row);
    return cell;
}

最佳答案

您正在为每一行创建一个单元格(UITableViewCell 的一个实例),而不是重新使用出列的单元格。 “dequeueReusableCell ...”是出于性能原因。用它来出列和重用其他行的单元格。

对所有单元格使用一个常量单元格标识符,并只修改每一行的单元格。即,更改图像或文本。但如果您想要任何性能改进,请重复使用这些单元格。这是要做的第一件事。然后,告诉我们性能是否足够,或者您是否需要更多优化。

关于ios - 带有 UIBlurEffect 的表格 View 变得迟钝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223733/

相关文章:

iphone - iOS5 数据存储问题。在哪里保存文件?

ios - 实例化 Nib 并访问其 subview

ios - 核心数据 - 选择特定列

iphone - 在按钮上滚动溢出文本的简单方法?

ios - 如何以编程方式为不同数量的 UIView 设置约束

objective-c - NSMutableDictionary 的 setObject 函数崩溃

ios - EXC_BAD_ACCESS KERN_INVALID_ADDRESS ios < 9.3 xcode 10

iOS 10 "Starting WebFilter logging for process"+ 从请求中删除代理授权 header

ios - 如何在 iOS 9 中打开电话拨号器?

ios - 离开 ViewController 时停止 AVCaptureSession