简体   繁体   中英

UICollectionview datas loading issue

Hi i have implemented UICollectionView in my app..If my array count value greater than 20 and when i tried to scroll the view it was not showing previous datas,,

In cellForItemAtIndexPath:(NSIndexPath *)indexPath method every time i check

  if (indexPath.row == [recipeImages count] - 1)
{
  [self loadDatas];

}

method.So that i could download 10 datas everytime...

  -(UICollectionViewCell *)collectionView:(UICollectionView*)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath

  {
static NSString *identifier = @"CourseList";

NSLog(@"indexpath %@ in cell for row",indexPath);
CollectionCellContent *cell = (CollectionCellContent*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSDictionary *course;
course=[courselist objectAtIndex:indexPath.row];
cell.coursename.text=[course objectForKey:@"course_name"];
cell.authorname.text=[course objectForKey:@"course_author"];
cell.price.text=[course objectForKey:@"course_price"];
cell.cover.image=[UIImage imageNamed:[course objectForKey:@"course_cover_image"]];
cell.review.image=[UIImage imageNamed:[course objectForKey:@"ratings"]];
NSString *imageUrlString = [[NSString alloc]initWithFormat:@"%@/%@/%@",delegate.course_image_url,[course objectForKey:@"course_id"],[course objectForKey:@"course_cover_image"]];

UIImage *imageFromCache = [self.imageCache objectForKey:imageUrlString];

if (imageFromCache) {
    cell.cover.image= imageFromCache;

}
else
{
    cell.cover.image = [UIImage imageNamed:@"placeholder"];


    [self.imageOperationQueue addOperationWithBlock:^{
        NSURL *imageurl = [NSURL URLWithString:imageUrlString];
        UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];

        if (img != nil) {


            [self.imageCache setObject:img forKey:imageUrlString];


            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                CollectionCellContent *updateCell = (CollectionCellContent*)[self.ipadcollection cellForItemAtIndexPath:indexPath];




                if (updateCell) {

                    [updateCell.cover setImage:img];
                }
            }];
        }
    }];
}


if (indexPath.row == [courselist count] - 1)
    [self loadDatas];
return cell;

}

in load datas method: [categorylist addObject:[arrayList1 objectForKey:@"category_name"]]; [category_tableView reloadData];

whenever i call reload data method i am facing this issue..

  -(void)loadDatas
  {
   NSString *urltemp=[[databaseurl sharedInstance]DBurl];
NSString *url1=@"AllCourse.php";

NSString *URLString=[NSString stringWithFormat:@"%@%@?offset=%d",urltemp,url1,offset];

NSMutableArray *search = [du MultipleCharacters:URLString];

NSDictionary* menu = [search valueForKey:@"serviceresponse"];

NSArray *Listofdatas=[menu objectForKey:@"Course List"];
NSMutableArray *temp1=[[NSMutableArray alloc]init];

if ([Listofdatas count]>0)
{


    for (int i=0;i<[Listofdatas count];i++)
    {
        NSDictionary *arrayList1= [Listofdatas objectAtIndex:i];
      NSDictionary* temp=[arrayList1 objectForKey:@"serviceresponse"];
        //            NSLog(@"Received Values %@",temp);
        if (offset==0) {
            [courselist addObject:temp];
        }
        else
        [temp1 addObject:temp];


    }


    if (offset!=0)
    {


    NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];

    for (NSInteger index =courselist.count; index < (courselist.count + temp1.count); index++) {
        [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
    }

    if (courselist) {
        [courselist addObjectsFromArray:temp1];
        [self.ipadcollection performBatchUpdates:^{
            [self.ipadcollection insertItemsAtIndexPaths:arrayWithIndexPaths];

        }

                                      completion:nil];
      //  [self.collectionView reloadData];

    }
    else {
        courselist = [[NSMutableArray alloc] initWithArray:temp1];


    }
    }
    if (![HUD isHidden]) {
        [HUD hide:YES];
    }

}
offset+=10;
[self.ipadcollection reloadData];

}

在重新加载UICollectionView之前要进行一些延迟。

[self performSelector:@selector(reloaddatas) withObject:nil afterDelay:0.5f];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM