【问题标题】:UICollectionview datas loading issueUICollectionview 数据加载问题
【发布时间】:2014-09-17 07:22:12
【问题描述】:

您好,我已经在我的应用程序中实现了 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];

}

【问题讨论】:

  • 能否请您也添加downloaddatas 方法?

标签: ios uicollectionview uicollectionviewcell


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多