由于可重用性问题,tableview单元格上的图像重复

时间:2016-03-28 09:33:46

标签: ios objective-c uitableview uiimageview

我有一个表格视图单元格,有标签和图像,滚动图像和标签变化,这是由于单元格出列功能。我试图通过将imageview设置为nil来解决它,但它没有工作。 重复的问题是使用PinImageView,它从一组图像中获取图像,而不是从URL中获取图像的图像。

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

    static NSString *cellIdentifier=@"cellIdentifier";
    TripInfoListCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil) {
        cell=(TripInfoListCell *)[[[NSBundle mainBundle] loadNibNamed:@"TripInfoListCell" owner:self options:nil] firstObject];

    }

   cell.PinImageView.image = nil;


    NSString *direction=[dict123 valueForKey:@"direction" ] ;
    NSString *drive_status=[dict123 valueForKey:@"driver_or_rider"];

    if((indexPath.row==totalRow-1)&&[direction isEqual:@"From"])
    {
        __weak TripInfoListCell *weakCell = cell;

        cell.PinImageView.image=[UIImage imageNamed:@"pin7@2x.png"];
       cell.userNameLabel.text=loggedUser.profile_name;

        cell.riderDriverSlNo.text=@"Driver";

        if([drive_status  isEqual:@"Ride"])

       {
           cell.riderDriverSlNo.text=@"Driver";
            cell.userNameLabel.text=@"Driver"; //In case of "Rider" "From" name in the last row
                  }
        else{
        cell.availabiltyInfoLabel.text=@"You";
        }

        NSString *imagePic = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_image"];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:imagePic]];

        [cell.userImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

            weakCell.userImageView.image=image;

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

            NSLog(@"%@",error);
        }];


    }
    else if((indexPath.row==totalRow-1)&&[direction isEqual:@"To"])

    {
        cell.PinImageView.image=[UIImage imageNamed:@"pin7@2x.png"];
        cell.userNameLabel.text=[_tripDetails objectForKey:@"park_name"];

        NSString *image=[NSString stringWithFormat:@"%@",[dict123 objectForKey:@"parkBannerImageUrl"]];

        [[ImageHandler sharedInstance] getImageFromURL:image withCompletion:^(UIImage *image) {

            if (image) {

                [cell.userImageView setImage:image];
            }
        }];
    }
    else
      if ([tableGoersList count] > indexPath.row)
        {

             NSDictionary *goersInfo=[tableGoersList objectAtIndex:indexPath.row];

        [[NSUserDefaults standardUserDefaults]setObject:[goersInfo valueForKey:@"user_image_url"] forKey:@"user_image"];
        [[NSUserDefaults standardUserDefaults]synchronize];

        __weak TripInfoListCell *weakCell = cell;

        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:goersInfo[@"user_image_url"]]];

        [cell.userImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

            weakCell.userImageView.image=image;

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

            NSLog(@"%@",error);
        }];

        if((indexPath.row==0)&&[direction isEqual:@"To"]) {

            if([drive_status  isEqual:@"Drive"]){

            cell.riderDriverSlNo.text=@"Driver";


            cell.PinImageView.image=[UIImage imageNamed:@"pin1@2x.png"];
            cell.userNameLabel.text=loggedUser.profile_name;
             cell.availabiltyInfoLabel.text=@"You";
            }
            else{
            }
        }
        else if((indexPath.row==0)&&[direction isEqual:@"From"]) {

            if([drive_status  isEqual:@"Drive"])
            {

            cell.PinImageView.image=[UIImage imageNamed:@"pin1@2x.png"];
            cell.userNameLabel.text=[_tripDetails objectForKey:@"park_name"];
            cell.riderDriverSlNo.text=@"";
            cell.availabiltyInfoLabel.text=@"";

            NSString *image=[NSString stringWithFormat:@"%@",[dict123 objectForKey:@"parkBannerImageUrl"]];

            [[ImageHandler sharedInstance] getImageFromURL:image withCompletion:^(UIImage *image) {

                if (image) {

                    [cell.userImageView setImage:image];
                }
            }];
            }
            else{
                NSLog(@"rider from");
                cell.riderDriverSlNo.text=@"";

            }

        }
        else{

            imageNameArray = [[NSArray alloc] initWithObjects:@"pin2@2x.png", @"pin3@2x.png", @"pin4@2x.png", @"pin5@2x.png", @"pin6@2x.png",@"pin8@2x.png",@"pin9@2x.png", nil];

            NSString* nameStr=[goersInfo valueForKey:@"name"];

           NSArray * nameStrArray = [nameStr componentsSeparatedByString: @"'s"];
            nameStr=[nameStrArray objectAtIndex:0];
            cell.riderDriverSlNo.text=[NSString stringWithFormat:@"Rider %ld",(long)indexPath.row];

            index = [TripInfoVC ifNameExists:_arrName Name:nameStr];

            if(index == 0)
            {
                cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:0]];
                 [_arrName addObject:nameStr];

            }
            if(index == 1)
            {
                cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:1]];
            }
            if(index == 2)
            {
                cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:2]];
            }

            if(index == 3)
            {
                cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:3]];
            }


            if(index == 4)
            {
                cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:4]];
            }

            if(index == 5)
            {
            cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:5]];
            }

            if(index == 6)
            {
            cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:6]];
            }

            if(index == 7)
            {
            cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:0]];
            }

            if(index == 8)
            {
           cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:1]];
            }

            if(index == 9)
            {
           cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:2]];
            }

            if(index == 10)
            {
            cell.PinImageView.image=[UIImage imageNamed:[imageNameArray objectAtIndex:3]];
            }

        }  // this pinImageView is the pin image which is repeated on scrolling

    return cell;
}

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您必须通过在操作队列中将代码添加到UICollection cellForRowAtIndexPath方法的委托方法中来实现飞行或延迟加载中的加载映像。

而且,如果你在UI上做了一些放在主线程中的东西,这是另一个例子:

使用SDWebImageManager在UICollectionView中延迟加载图像

答案 1 :(得分:0)

你可能面临关闭碰撞。当你声明完成块

[cell.userImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

            weakCell.userImageView.image=image;

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

            NSLog(@"%@",error);
        }];

weakCell是UITableView委托请求的单元格对象的指针。它可能会在您滚动表格后重复使用,此时您的完成块实际上可能会被调用,因为下载 过程不是即时的。

如何处理?我会使用UITableViewCell.tag属性来识别我需要单元格时请求的确切图像,并且它与我在完成块运行时尝试放入该单元格的图像相匹配。也许其他一些下载后来被解雇并覆盖了标签属性。