ios - uitableview 滚动期间滞后

标签 ios objective-c uitableview

滚动时我的桌面 View 出现滞后。我使用了自定义单元格,因为单元格中有许多内容 View 。但是当我滚动 tableview 时它滚动不顺畅。我不明白我的编码有什么问题,请帮助我找到问题。这是我的代码:

   static NSString *CellIdentifier = @"Cell1";
    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil)
        {
            cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.backgroundColor=[UIColor blackColor];
            cell.frame = CGRectMake(0.0, 0.0, 320.0, 200.0);
            UITapGestureRecognizer *tap1 =
            [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)];
            tap1.delegate=self;
            tap1.numberOfTapsRequired=1;
            [cell.image1 addGestureRecognizer:tap1];
            [tap1 release];

            UITapGestureRecognizer *tap2 =
            [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)];
            tap2.delegate=self;
            tap2.numberOfTapsRequired=1;
            [cell.image2 addGestureRecognizer:tap2];
            [tap2 release];

        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.image1 setTag:indexPath.row*2];
        [cell.image2 setTag:indexPath.row*2+1];
        NSString *checkfirstcat=[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"category"];
        NSString *checksecondcat=[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"category"];
            for (int i = 0; i < [categories count]; i++) {
                if ([checkfirstcat isEqualToString:[categories objectAtIndex:i]]) {
                    cell.first.backgroundColor=[uniquecolors objectAtIndex:i];
                    cell.veryfirst.backgroundColor=[uniquecolors objectAtIndex:i];
                    break;
                }
            }

        for (int i = 0; i < [categories count]; i++) {
            if ([checksecondcat isEqualToString:[categories objectAtIndex:i]]) {
                cell.second.backgroundColor=[uniquecolors objectAtIndex:i];
                cell.verysecond.backgroundColor=[uniquecolors objectAtIndex:i];
                break;  
            }
        }

        cell.veryfirst.text= [NSString stringWithFormat:@"   %@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"rank"]];
        cell.first.text=[NSString stringWithFormat:@" %@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"category"]];
        cell.verysecond.text=[NSString stringWithFormat:@"   %@", [[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"rank"]];
        cell.second.text=[NSString stringWithFormat:@" %@",[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"category"]];

        UIActivityIndicatorView *spinner1 =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        spinner1.frame = CGRectMake(55, 55, 40, 40);
        [cell.contentView addSubview:spinner1];    
        [spinner1 startAnimating];

        UIActivityIndicatorView *spinner2 =   [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        spinner2.frame = CGRectMake(220, 55, 40, 40); 
        [cell.contentView addSubview:spinner2];            
        [spinner2 startAnimating];

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0ul);
                dispatch_async(queue, ^{
        NSString *pathimage1 = [[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"];
        NSString *pathimage2 = [[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"];
                dispatch_sync(dispatch_get_main_queue(), ^{
        [cell.image1 setImageWithURL:[NSURL URLWithString:pathimage1] placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(UIImage *image, BOOL cached){
                        [spinner1 stopAnimating];
                    } failure:^(NSError *error) {
                        [spinner1 stopAnimating];
                    }];

        [cell.image2 setImageWithURL:[NSURL URLWithString:pathimage2] success:^(UIImage *image, BOOL cached) {
            [spinner2 stopAnimating];
        } failure:^(NSError *error) {
            [spinner2 stopAnimating];
        }];
        });
       });

        cell.btn1.tag=indexPath.row*2;
        cell.btn2.tag=indexPath.row*2+1;

        [cell.btn1 setTitle:[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"total_like"] forState:UIControlStateNormal];
        [cell.btn2 setTitle:[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"total_like"] forState:UIControlStateNormal];
        NSString *btn1status=[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"like_stat"];
        NSString *btn2status=[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"like_stat"];

        if (![btn1status isEqualToString:@"false"]) {
            [cell.btn1 setImage:[UIImage imageNamed: @"fav-icon.png"] forState:UIControlStateNormal];
        }
        else
        {
            [cell.btn1 setImage:[UIImage imageNamed: @"fav.png"] forState:UIControlStateNormal];
        }

        if (![btn2status isEqualToString:@"false"]) {
            [cell.btn2 setImage:[UIImage imageNamed: @"fav-icon.png"] forState:UIControlStateNormal];
        }
        else
        {
            [cell.btn2 setImage:[UIImage imageNamed: @"fav.png"] forState:UIControlStateNormal];
        }
        [cell.btn1 addTarget:self action:@selector(like1:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn2 addTarget:self action:@selector(like2:) forControlEvents:UIControlEventTouchUpInside];


        }
    return cell;

}

最佳答案

我猜您是在 tableView:cellForRowAtIndexPath: 方法中完成所有这些操作?这通常是不好的。特别是创建诸如 UIActivityIndi​​catorView View 等内容。

请记住,每次屏幕上出现一行时都会调用此方法。 即使您向下滚动,然后再次向上滚动。 所以这个方法将会被每一行调用很多次。

如果您想添加微调器,在 Interface Builder 中进行配置会更明智,然后只调用微调器来设置动画。

关于ios - uitableview 滚动期间滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110353/

相关文章:

ios - 是否可以通过应用内购买来销售其他应用程序

ios - UINavigationController 不会推送其他 View Controller ?

iOS 11 动画大标题显示模式

ios - 将数据传递到AWS API Gateway时如何倾斜?

ios - 使用临时存储打开文件

iphone - 如何在 iphone 中找到图像的颜色边缘?

ios - 为 iOS 中的特定范围向 NSMutableAttributedString 添加属性时发生崩溃

ios - UITableViewController 和 UICollectionViewController 的 .view 属性

iphone - 像前后一样用两个摄像头拍照

ios - 警告 : Attempt to dismiss from view controller <UINavigationController: 0xb359a20> while a presentation or dismiss is in progress