我滚动浏览UITableView有点不稳定,我怎么能让它流畅?

时间:2016-11-02 08:42:02

标签: ios objective-c uitableview

在滚动时,滚动表格有点不稳定和断断续续。

我能做些什么改进才能让它顺利进行?

这是我的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    UITableViewCell *cellEmpty;

    if ([tableView isEqual:self.categoryTableView]) {

        NSString *CellIdentifierCategory = @"categoryCell";
        UITableViewCell *categoryCell = [self.categoryTableView dequeueReusableCellWithIdentifier:CellIdentifierCategory];

        if (categoryCell == nil) {
            categoryCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierCategory];
        }
        else{

            categoryCell.textLabel.text = [_categoryTableArray objectAtIndex:indexPath.row];

            return categoryCell;
        }

    }

    if ([tableView isEqual:self.tableView]) {

        if (indexPath.section == self.objects.count) {
            UITableViewCell *cell = [self tableView:tableView cellForNextPageAtIndexPath:indexPath];

            UILabel *loadLabel = (UILabel *)[cell viewWithTag:1];

            if (!self.isLoading) {

                loadLabel.text = @"The End of Space";
                self.tableView.tableFooterView = nil;
            }

            cell.userInteractionEnabled = NO;

            return cell;

        }


        NSString *CellIdentifier = @"PhotoCell";
        InterestsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[InterestsCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        else{

            PFUser *user = [object objectForKey:@"userTookPhoto"];


            dispatch_async(dispatch_get_main_queue(), ^{
                cell.companyLogo.layer.cornerRadius = cell.companyLogo.frame.size.width / 2;
                cell.companyLogo.clipsToBounds = YES;
            });

            [cell.productImageView setUserInteractionEnabled:YES];
            [cell.priceLabel setUserInteractionEnabled:YES];

            UITapGestureRecognizer *tapForImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleProductImageTap:)];
            tapForImage.cancelsTouchesInView = YES;
            tapForImage.numberOfTapsRequired = 1;
            [cell.productImageView addGestureRecognizer:tapForImage];

            UITapGestureRecognizer *tapForPrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleProductImageTap:)];
            tapForPrice.cancelsTouchesInView = YES;
            tapForPrice.numberOfTapsRequired = 1;
            [cell.priceLabel addGestureRecognizer:tapForPrice];

            cell.productImageView.tag = indexPath.section;


            cell.productImageView.file = (PFFile *)object[@"image"];
            [cell.productImageView loadInBackground:^(UIImage * _Nullable image, NSError * _Nullable error) {

            } progressBlock:^(int percentDone) {
                NSLog(@"float value: %f",percentDone * 0.01);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [cell.progressView setProgress:percentDone * 0.01 animated:YES];
                });
                if (percentDone == 100) {
                    [cell.progressView setHidden:YES];
                }

            }];

            dispatch_async(dispatch_get_main_queue(), ^{
                cell.productTitle.text = object[@"titleOfPhoto"];
            });

            [cell.companyLogo setUserInteractionEnabled:YES];

            UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(segueCompanyProfile)];

            tapGesture1.numberOfTapsRequired = 1;

            [cell.companyLogo addGestureRecognizer:tapGesture1];



            if (user[@"profileImage"]) {
                cell.companyLogo.file = (PFFile *)user[@"profileImage"];
                [cell.companyLogo loadInBackground];
            }
            else{

                dispatch_async(dispatch_get_main_queue(), ^{
                    UIImage *image = [UIImage imageNamed:@"defaultBrandLogo.png"];
                    cell.companyLogo.image = image;
                });
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                [cell.companyLabelButton setTitle:user.username forState:UIControlStateNormal];
                cell.companyLabelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                cell.companyLabelButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            });
            [cell.companyLabelButton addTarget:self action:@selector(segueCompanyProfile) forControlEvents:UIControlEventTouchUpInside];


            NSNumber *price = object[@"PriceOfPhoto"];


            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
            NSString *numberAsString = [numberFormatter stringFromNumber:price];

            NSString *priceLabelText = [NSString stringWithFormat:@"%@",numberAsString];

            dispatch_async(dispatch_get_main_queue(), ^{

                cell.priceLabel.text = priceLabelText;
            });

            //        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, cell.productTitle.frame.origin.y + cell.productTitle.frame.size.height + 10, self.view.frame.size.width - 30, 2)];
            //        line.backgroundColor = [UIColor groupTableViewBackgroundColor];
            //        [cell.cardView addSubview:line];

            cell.likeButtonOutlet.delegate = self;
            cell.likeButtonOutlet.sectionIndex = indexPath.section;

            NSArray *likesArray = object[@"likesBy"];

            if (likesArray.count > 0) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
                [formatter setGroupingSeparator:groupingSeparator];
                [formatter setGroupingSize:3];
                [formatter setAlwaysShowsDecimalSeparator:NO];
                [formatter setUsesGroupingSeparator:YES];
                NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:likesArray.count]];

                dispatch_async(dispatch_get_main_queue(), ^{
                    cell.likeCountLabel.text = formattedString;
                });
            }
            else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    cell.likeCountLabel.text = nil;
                });
            }

            if (likesArray.count > 0) {
                for (NSString *likes in likesArray) {
                    if ([likes isEqualToString:[PFUser currentUser].objectId]) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [cell.likeButtonOutlet setSelected:YES];
                        });
                    }
                    else{
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [cell.likeButtonOutlet setSelected:NO];
                        });
                    }
                }
            }
            else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [cell.likeButtonOutlet setSelected:NO];
                });
            }


        }

        return cell;
    }


    return cellEmpty;
}

0 个答案:

没有答案