UICollectionview数据加载问题

时间:2014-09-17 07:22:13

标签: ios uicollectionview uicollectionviewcell

您好我已经在我的应用程序中实现了UICollectionView。如果我的数组计数值大于20,当我试图滚动视图时它没有显示以前的数据,

每次检查时,在cellForItemAtIndexPath:(NSIndexPath *)indexPath方法

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

}

方法。所以我每次都可以下载10个数据......

  -(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;

}

在加载数据方法中:     [categorylist addObject:[arrayList1 objectForKey:@“category_name”]];     [category_tableView reloadData];

无论何时我调用重载数据方法我都面临这个问题..

  -(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];

}

1 个答案:

答案 0 :(得分:2)

在重新加载UICollectionView之前做一些延迟。

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