• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 无法从 json 响应中设置部分中的 UICollectionView 项目数

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:12:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我对 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap