OGeek|极客世界-中国程序员成长平台

标题: ios - 无法从 json 响应中设置部分中的 UICollectionView 项目数 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:12
标题: ios - 无法从 json 响应中设置部分中的 UICollectionView 项目数

我对 iOS 编程非常陌生,正在尝试从 json 响应中设置 UICollectionView 项目。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self downloadJSONData];

    /* uncomment this block to use subclassed cells */
    [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier"cvCell"];
    /* end of subclass-based cells block */

    // Configure layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:CGSizeMake(150, 150)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:flowLayout];
    [self.collectionView reloadData];
}

-(void) downloadJSONData {
    NSURL *serviceUrl = [[NSURL alloc]initWithString"http://myurlhere.com/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:serviceUrl];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [conn start];
}

-(void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response
{
    self.data = [[NSMutableData alloc]init];
}

-(void)connectionNSURLConnection *)connection didReceiveDataNSData *)responsedata {
    [self.data appendData:responsedata];
}

-(void)connectionNSURLConnection *)connection didFailWithErrorNSError *)error {
    NSLog(@"Error : %@", error);
}

-(void)connectionDidFinishLoadingNSURLConnection *)connection {
    NSLog(@"Succeeded! Received %d bytes of data",[self.data length]);
    NSString *txt = [[NSString alloc] initWithData:self.data encoding: NSASCIIStringEncoding];
    NSLog(@"response data %@", txt);
    self.dictionaryData = [txt JSONValue];
}

-(NSInteger)numberOfSectionsInCollectionViewUICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section {

    //NSMutableArray *sectionArray = [self.dataArray objectAtIndex:section];
    self.jsonArray = [self.dictionaryData objectForKey"images"];
    return [self.jsonArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Setup cell identifier
    static NSString *cellIdentifier = @"cvCell";

    /*  Uncomment this block to use nib-based cells */
    // UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    // UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    // [titleLabel setText:cellData];
    /* end of nib-based cell block */

    /* Uncomment this block to use subclass-based cells */
    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
    NSString *cellData = [data objectAtIndex:indexPath.row];
    [cell.titleLabel setText:cellData];
    /* end of subclass-based cells block */

    // Return the cell
    return cell;
}

当我运行代码时,它什么也不显示。我想要的是它显示项目并且数量等于数组的计数。还有一件事,关于如何将图像异步加载到 Collection View 中的任何建议?

对不起我的英语。如果有人可以帮助我,我会很高兴。

谢谢。干杯!



Best Answer-推荐答案


connectionDidFinishLoading方法中添加:

[self.collectionView reloadData];

对于 Asyn 图像加载,您可以检查以下任何一项,

  1. SDWebImage
  2. AsyncImageView
  3. LazyTableImages - 它适用于 UITableView,但您可以对 UICollectionView 使用相同的概念

关于ios - 无法从 json 响应中设置部分中的 UICollectionView 项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126918/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4