具有UIBlurEffect的表视图变得滞后

时间:2014-12-01 07:40:48

标签: ios objective-c iphone uitableview

我有桌面视图的问题,在使用uiblurview tableview实现顶部栏后,iphone5上的延迟滚动tableview需要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;
}

1 个答案:

答案 0 :(得分:1)

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

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