动态填充时,集合视图单元格会闪烁

时间:2017-12-28 07:25:23

标签: ios objective-c json uicollectionview uicollectionviewcell

我在视图上有三个集合视图,标题集合视图,外集合视图和内集合视图...自定义单元格分别是recipe-heading和innercell。内部集合视图位于外部集合视图内。

外部集合视图有8个单元格(取决于recipetypeArray),内部单元格有25个单元格(取决于配方响应数组)。当outercell1移动到outercell2时,标题单元格1也应该移动到headingcell2,依此类推。内部单元格是动态的,因此它应该根据从服务器获取的类别ID(外部集合视图的当前单元格),第1页和限制25(暂时静态地坐标)收到的响应而不断变化。

问题 细胞上的图像(内部细胞)不断变换,一次显示不同的图像,导致闪烁效果。即使细胞上的标签也在不断变化,这绝对是错误的。

代码完成如下: RecipesViewController

- (void)viewDidLoad{
        [self callService:@0]; }

-(void)callService:(NSNumber*)categoryid{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];//for server hit
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    [manager POST:@"https://www.foodfuels.com/Api/getrecipes" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        responseArray = nil;
        responseArray = [responseObject valueForKey:@"data"];
        responseDict = responseObject;
        dispatch_async(dispatch_get_main_queue(), ^{
            [_outerCollectionView reloadData];
            [outerCell.innerCollectionView reloadData];
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }); }

          failure:^(NSURLSessionDataTask  *_Nullable task, NSError  *_Nonnull error) {
              [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                           }]; }
- (void)collectionView:(UICollectionView *)collectionView
       willDisplayCell:(UICollectionViewCell *)cell
    forItemAtIndexPath:(NSIndexPath *)indexPath{
    if(collectionView==_outerCollectionView){ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        [self callService:rowValue];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_headingCollectionView reloadData];
            [_headingCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        });
     });
    }
}

RecipeInnerCell

  -(void)populateRecipeScreen:(NSArray*)recipeResponseArr index:(NSIndexPath*)path{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            NSLog(@"value of indexpath.row is %ld",path.row);

            dispatch_sync(dispatch_get_main_queue(), ^{
                if([[[recipeResponseArr valueForKey:@"description"]objectAtIndex:path.row]isKindOfClass:[NSString class]]){

                    self.titleLbl.text =[[recipeResponseArr valueForKey:@"description"]objectAtIndex:path.row];
                  }
                else{
                    self.titleLbl.text =@"";

                }
                if([[[[recipeResponseArr valueForKey:@"user"]valueForKey:@"username"]  objectAtIndex:path.row]isKindOfClass:[NSString class]]){

                 self.subTitleLbl.text = [[[recipeResponseArr valueForKey:@"user"]valueForKey:@"username"]  objectAtIndex:path.row];
                }
                else{
                   self.subTitleLbl.text=@"";
                }
                //-----like_count---//
                if(![[[[recipeResponseArr valueForKey:@"like_count"]objectAtIndex:path.row]stringValue]isKindOfClass:[NSNull class]]){

                self.like_Count.text = [[[recipeResponseArr valueForKey:@"like_count"]objectAtIndex:path.row]stringValue];
                }
                else{
                    self.like_Count.text=@"";
                }
                if(![[[[recipeResponseArr valueForKey:@"comment_count"]objectAtIndex:path.row]stringValue]isKindOfClass:[NSNull class]]){

                self.comment_Count.text =[[[recipeResponseArr valueForKey:@"comment_count"]objectAtIndex:path.row]stringValue];
                }else{
                    self.comment_Count.text=@"";
                }
                if(![[[[recipeResponseArr valueForKey:@"share_count"]objectAtIndex:path.row]stringValue]isKindOfClass:[NSNull class]]){

                self.share_Count.text = [[[recipeResponseArr valueForKey:@"share_count"]objectAtIndex:path.row]stringValue];
                }
                else{
                    self.share_Count.text=@"";
                }
                if(![[[recipeResponseArr  valueForKey:@"user"]valueForKey:@"image"]isKindOfClass:[NSNull class]]){
                    [UIView setAnimationsEnabled:NO];

                [self.smallImgView sd_setImageWithURL:[NSURL   URLWithString:[[[recipeResponseArr  valueForKey:@"user"]valueForKey:@"image"]objectAtIndex:path.row]]
                                     placeholderImage:[UIImage imageNamed:@"user-profile-128.png"]];
                }
                else{
                     self.smallImgView.image=[UIImage imageNamed:@"user-profile-128.png"];
                }   

                if(![[[[recipeResponseArr valueForKey:@"upload_images"]objectAtIndex:path.row]valueForKey:@"name"]isKindOfClass:[NSNull class]]){
                    [UIView setAnimationsEnabled:NO];

                [self.mainImg sd_setImageWithURL:[NSURL   URLWithString:[[[[recipeResponseArr valueForKey:@"upload_images"]objectAtIndex:path.row]valueForKey:@"name"]firstObject]]
                                     placeholderImage:[UIImage imageNamed:@"user-profile-128.png"]];
                }
                else{
                  self.mainImg.image=[UIImage imageNamed:@"user-profile-128.png"];
                }

                if(![[[[[[recipeResponseArr valueForKey:@"comments"]objectAtIndex:path.row]firstObject]valueForKey:@"user"]valueForKey:@"image"]isKindOfClass:[NSNull class]]){
                    [UIView setAnimationsEnabled:NO];

                    [self.cmntImg sd_setImageWithURL:[NSURL URLWithString:[[[[[recipeResponseArr valueForKey:@"comments"]objectAtIndex:path.row]firstObject]valueForKey:@"user"]valueForKey:@"image"]]
                                    placeholderImage:[UIImage imageNamed:@"user-profile-128.png"]];
                }
                else{
                    self.cmntImg.image=[UIImage imageNamed:@"user-profile-128.png"];
                }

                //-----comment-----//
                if([[[[recipeResponseArr valueForKey:@"comments"]objectAtIndex:path.row]valueForKey:@"comment"]isKindOfClass:[NSString class]]){
                    [UIView setAnimationsEnabled:NO];
                    self.cmntSubtitle.text = [[[[recipeResponseArr valueForKey:@"comments"]objectAtIndex:path.row]valueForKey:@"comment"]objectAtIndex:path.row];
                  }
                else{
                    self.cmntSubtitle.text =@"";


                }


            });
        });
    }

的cellForRowAtIndexPath

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(collectionView==_headingCollectionView){
         recipeHeading = (RecipeHeadingCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"headingCell" forIndexPath:indexPath];
         _headingCollectionView.delegate = self;
        [UIView setAnimationsEnabled:NO];
        [recipeHeading.headingBtn setTitle:[recipeTypeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
       [recipeHeading.headingBtn layoutIfNeeded];


        [recipeHeading.headingBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];


        bool selected = [allValues[indexPath.row] boolValue];

        if(selected)
        {[recipeHeading.headingBtn setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];            [recipeHeading.headingBtn addSubview:bottomBorder];
        }
        else
        {
        [recipeHeading.headingBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];

        }

        return recipeHeading;
    }
    else if (collectionView==_outerCollectionView){
        outerCell =(RecipeOuterCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"outerCell" forIndexPath:indexPath];
        collectionView.pagingEnabled = true;

        return outerCell;
    }
    else{


        static NSString *simpleTableIdentifier = @"innerCell";

        RecipeInnerCell *innerCell = (RecipeInnerCell*)[collectionView dequeueReusableCellWithReuseIdentifier:simpleTableIdentifier forIndexPath:indexPath];
        // collectionView.pagingEnabled = true;
        [innerCell populateRecipeScreen:responseArray index:indexPath];
        return innerCell;

    }
}

JSON数据

<__NSArrayI 0x6040007a4520>(
{
    category = "<null>";
    "category_id" = "\"1\", \"3\", \"6\"";
    "comment_count" = 1;
    comments =     (
                {
            comment = "I had this for lunch so delicious.  I want it again for dinner";
            created = "2017-04-27T15:14:02-0500";
            "feed_id" = 0;
            id = 1726;
            modified = "2017-04-27T15:14:02-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1493324042;
            user =             {
                "first_name" = Jill;
                "group_id" = 3;
                id = 3223;
                image = "https://www.foodfuels.com/media/user/3223.png";
                "last_name" = Gillette;
                username = Jillette;
            };
            "user_id" = 3223;
        }
    );
    created = "2016-03-22T01:48:28-0500";
    description = "Tuna, Egg, Avocado Salad";
    directions = "Mash together all of the ingredients and serve.";
    id = 38;
    ingredients = "\U2022 1 can tuna
\n\U2022 1 hard boiled egg
\n\U2022 \U00bd avocado sliced
\n\U2022 1 teaspoon freshly squeezed lime or lemon juice";
    "like_count" = 31;
    likes =     (
                {
            created = "2016-05-01T09:04:32-0500";
            "feed_id" = "<null>";
            id = 369;
            modified = "2016-05-01T09:04:32-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1462111472;
            user =             {
                "first_name" = Kristine;
                "group_id" = 3;
                id = 1088;
                image = "https://graph.facebook.com/10209172098450544/picture?type=square";
                "last_name" = Demchuk;
                username = "Kristine Demchuk";
            };
            "user_id" = 1088;
        },
                {
            created = "2016-12-03T16:03:59-0500";
            "feed_id" = "<null>";
            id = 2446;
            modified = "2016-12-03T16:03:59-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1480799039;
            user =             {
                "first_name" = Maureen;
                "group_id" = 3;
                id = 2198;
                image = "2198.jpg";
                "last_name" = Miller;
                username = Mcm;
            };
            "user_id" = 2198;
        },
                {
            created = "2017-01-02T21:23:36-0500";
            "feed_id" = "<null>";
            id = 2648;
            modified = "2017-01-05T06:41:51-0500";
            "recipe_id" = 38;
            "status_id" = 0;
            timestamp = 1483616511;
            user =             {
                "first_name" = charleyne;
                "group_id" = 3;
                id = 2335;
                image = "2335.jpg";
                "last_name" = cawn;
                username = sherrislp;
            };
            "user_id" = 2335;
        },
                {
            created = "2017-01-07T23:24:28-0500";
            "feed_id" = "<null>";
            id = 2741;
            modified = "2017-01-07T23:24:28-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1483849468;
            user = "<null>";
            "user_id" = 2325;
        },
                {
            created = "2017-01-08T20:37:54-0500";
            "feed_id" = "<null>";
            id = 2755;
            modified = "2017-01-08T20:37:54-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1483925874;
            user =             {
                "first_name" = Jillion;
                "group_id" = 3;
                id = 2412;
                image = "2412.jpeg";
                "last_name" = Eskra;
                username = "Jillion Eskra";
            };
            "user_id" = 2412;
        },
                {
            created = "2017-02-05T08:57:23-0500";
            "feed_id" = "<null>";
            id = 2986;
            modified = "2017-02-05T08:57:23-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1486303043;
            user =             {
                "first_name" = Melissa;
                "group_id" = 3;
                id = 2559;
                image = "";
                "last_name" = "Gliottoni ";
                username = "";
            };
            "user_id" = 2559;
        },
                {
            created = "2017-03-04T17:54:07-0500";
            "feed_id" = "<null>";
            id = 3174;
            modified = "2017-03-04T17:54:13-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1488668053;
            user =             {
                "first_name" = HEATHER;
                "group_id" = 3;
                id = 2676;
                image = "2676.jpg";
                "last_name" = KUNTZ;
                username = Hkntz83;
            };
            "user_id" = 2676;
        },
                {
            created = "2017-03-23T09:08:04-0500";
            "feed_id" = "<null>";
            id = 3510;
            modified = "2017-03-23T09:08:04-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1490278084;
            user =             {
                "first_name" = Jamie;
                "group_id" = 2;
                id = 1978;
                image = "1978.jpg";
                "last_name" = Smith;
                username = Jamela321;
            };
            "user_id" = 1978;
        },
                {
            created = "2017-04-15T14:06:14-0500";
            "feed_id" = "<null>";
            id = 3781;
            modified = "2017-04-15T14:06:14-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1492283174;
            user = "<null>";
            "user_id" = 1589;
        },
                {
            created = "2017-07-09T23:46:37-0500";
            "feed_id" = "<null>";
            id = 4733;
            modified = "2017-07-09T23:46:37-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1499661997;
            user =             {
                "first_name" = Asia;
                "group_id" = 3;
                id = 3870;
                image = "";
                "last_name" = Ferraro;
                username = ainegron;
            };
            "user_id" = 3870;
        },
             {
            created = "2017-08-11T07:27:02-0500";
            "feed_id" = "<null>";
            id = 5002;
            modified = "2017-08-11T07:27:02-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1502454422;
            user =             {
                "first_name" = Maria;
                "group_id" = 3;
                id = 4050;
                image = "4050.jpg";
                "last_name" = Vuggica;
                username = mamamaria15;
            };
            "user_id" = 4050;
        },
                {
            created = "2017-08-11T14:58:00-0500";
            "feed_id" = "<null>";
            id = 5005;
            modified = "2017-08-11T14:58:05-0500";
            "recipe_id" = 38;
            "status_id" = 0;
            timestamp = 1502481485;
            user =             {
                "first_name" = Marjorie;
                "group_id" = 3;
                id = 4059;
                image = "4059.jpg";
                "last_name" = Justic;
                username = mags23;
            };
            "user_id" = 4059;
        },
                {
            created = "2017-08-12T08:57:03-0500";
            "feed_id" = "<null>";
            id = 5026;
            modified = "2017-08-12T08:57:10-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1502546230;
            user =             {
                "first_name" = Julie;
                "group_id" = 3;
                id = 2583;
                image = "";
                "last_name" = Barwegen;
                username = barwegenj;
            };
            "user_id" = 2583;
        },
                {
            created = "2017-08-13T07:48:23-0500";
            "feed_id" = "<null>";
            id = 5027;
            modified = "2017-08-13T07:48:23-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1502628503;
            user =             {
                "first_name" = Scott;
                "group_id" = 3;
                id = 4138;
                image = "";
                "last_name" = Williams;
                username = Scott;
            };
            "user_id" = 4138;
        },
                {
            created = "2017-08-18T09:56:33-0500";
            "feed_id" = "<null>";
            id = 5057;
            modified = "2017-08-18T09:56:43-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1503068203;
            user =             {
                "first_name" = Kathy;
                "group_id" = 3;
                id = 4190;
                image = "4190.jpg";
                "last_name" = Longtin;
                username = Klongtin;
            };
            "user_id" = 4190;
        },
                {
            created = "2017-08-25T20:07:07-0500";
            "feed_id" = "<null>";
            id = 5093;
            modified = "2017-08-25T20:07:21-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1503709641;
            user =             {
                "first_name" = Amy;
                "group_id" = 3;
                id = 4193;
                image = "4193.jpg";
                "last_name" = Bettenhausen;
                username = Amy2675;
            };
            "user_id" = 4193;
        },
                {
            created = "2017-10-04T20:49:27-0500";
            "feed_id" = "<null>";
            id = 5468;
            modified = "2017-10-04T20:49:27-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1507168167;
            user =             {
                "first_name" = Tricia;
                "group_id" = 3;
                id = 4663;
                image = "thumb_4663.jpg";
                "last_name" = Nagel;
                username = mstrishy;
            };
            "user_id" = 4663;
        },
                {
            created = "2017-10-26T19:41:09-0500";
            "feed_id" = "<null>";
            id = 5674;
            modified = "2017-10-26T19:41:09-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1509064869;
            user =             {
                "first_name" = Charlyn;
                "group_id" = 3;
                id = 3383;
                image = "";
                "last_name" = Nutter;
                username = nutterc;
            };
            "user_id" = 3383;
        },
                {
            created = "2017-11-03T15:55:20-0500";
            "feed_id" = "<null>";
            id = 5749;
            modified = "2017-11-03T15:55:20-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1509742520;
            user =             {
                "first_name" = "Jessica ";
                "group_id" = 3;
                id = 4969;
                image = "";
                "last_name" = Turf;
                username = jbt2017;
            };
            "user_id" = 4969;
        },
                {
            created = "2017-11-07T18:35:39-0500";
            "feed_id" = "<null>";
            id = 5788;
            modified = "2017-11-07T18:35:45-0500";
            "recipe_id" = 38;
            "status_id" = 0;
            timestamp = 1510097745;
            user =             {
                "first_name" = Tracy;
                "group_id" = 3;
                id = 4985;
                image = "";
                "last_name" = McKenney;
                username = Treyzamay;
            };
            "user_id" = 4985;
        },
                {
            created = "2017-11-08T10:46:46-0500";
            "feed_id" = "<null>";
            id = 5794;
            modified = "2017-11-08T10:46:46-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1510156006;
            user =             {
                "first_name" = Kathryn;
                "group_id" = 3;
                id = 4739;
                image = "4739.jpg";
                "last_name" = Northcott;
                username = "Fotografer ";
            };
            "user_id" = 4739;
        },
                {
            created = "2017-11-11T10:27:40-0500";
            "feed_id" = "<null>";
            id = 5808;
            modified = "2017-11-11T10:27:40-0500";
            "recipe_id" = 38;
            "status_id" = 1;
            timestamp = 1510414060;
            user =             {
                "first_name" = Jeff;
                "group_id" = 3;
                id = 4357;
                image = "4357.jpg";
                "last_name" = Worsham;
                username = Jworsham;
            };
            "user_id" = 4357;
        }
    );
    modified = "2016-03-22T01:48:28-0500";
    notes = "";
    "preparation_time" = "5 minutes";
    rating = 0;
    "serving_size" = "Male 8oz., Female 4oz.";
    "share_count" = 14;
    "status_id" = 1;
    title = "Tuna, Egg, Avocado Salad";
    "upload_images" =     (
                {
            "feed_id" = 0;
            id = 41;
            name = "https://www.foodfuels.com/media/dish/01314127911-1458611308.jpg";
            "recipe_id" = 38;
            type = dish;
            "user_id" = 25;
        }
    );
    user =     {
        email = "aaron@foodfuelsweightloss.com";
        "first_name" = Aaron;
        id = 25;
        image = "https://www.foodfuels.com/media/user/25.png";
        "last_name" = Boniecki;
        username = "Aaron Boniecki";
    };
    "user_id" = 25;
}
)

我的代码出了什么问题?

观点:

Image attached to describe the view exactly

0 个答案:

没有答案