来自 JSON 的数据未在 UICollectionView 中更新

Posted

技术标签:

【中文标题】来自 JSON 的数据未在 UICollectionView 中更新【英文标题】:Data from JSON not updating in UICollectionView 【发布时间】:2015-12-08 17:25:23 【问题描述】:

我正在创建一个将 Newsfeed 作为 UICollectionView 的应用程序,但是当我更改 JSON 文件时它似乎没有更新。我正在使用 UIRefreshControl 来刷新它,但我不知道我的问题是与此有关还是与 JSON 的读取方式有关(或完全与其他问题有关)。

viewDidLoad

- (void)viewDidLoad

    [super viewDidLoad];

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    self.navigationItem.title = @"News";

    UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0,0,23,16);
    [btn setBackgroundImage:[UIImage imageNamed:@"menuImage.png"] forState:UIControlStateNormal];
    [btn addTarget:(NavigationViewController *)self.navigationController action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barBtn;

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    _session = [NSURLSession sessionWithConfiguration:config
                                             delegate:self
                                        delegateQueue:nil];
    [self fetchFeed];



    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat frameWidth = screenRect.size.width - 20;
    CGFloat frameHeight = screenRect.size.height - 20;

    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, frameWidth, frameHeight) collectionViewLayout:layout];
    [_collectionView setDataSource: self];
    [_collectionView setDelegate: self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor clearColor]];


    [self.view addSubview:_collectionView];



    UIRefreshControl * refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refresh Images"];
    [_collectionView addSubview:refreshControl];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

    [self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
    [self.collectionView reloadData];


fetchFeed

- (void)fetchFeed

    NSString *requestString = @"http://www.jameslester.xyz/example.json";
    NSURL *url = [NSURL URLWithString:requestString];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
                                        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                                                                   options:0
                                                                                                     error:nil];
                                        self.articles = jsonObject[@"articles"];

                                        NSLog(@"%@", self.articles);
                                        NSLog(@"Feed Fetched!!!");

                                        dispatch_async(dispatch_get_main_queue(), ^[self.collectionView reloadData];
                                    );
                                    ];


    [dataTask resume];

刷新

- (void)refresh:(id)sender


    [self fetchFeed];

    [(UIRefreshControl *)sender endRefreshing];

    NSLog(@"Refreshed");

任何帮助将不胜感激。

集合查看数据源

#define LABEL_TAG 100001

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

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    UILabel *articleTitle = [cell.contentView viewWithTag:LABEL_TAG];
    NSDictionary *article = self.articles[indexPath.row];

    if (!articleTitle) 
        articleTitle = [[UILabel alloc]initWithFrame:CGRectMake(5, cell.bounds.size.height - cell.bounds.size.height / 2.2, cell.bounds.size.width - 10, cell.bounds.size.height / 2)];
        articleTitle.textColor = [UIColor whiteColor];
        articleTitle.numberOfLines = 3;
        articleTitle.adjustsFontSizeToFitWidth = YES;
        articleTitle.tag = LABEL_TAG;
        [cell.contentView addSubview:articleTitle];
    

    articleTitle.text = article[@"title"];

    NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: article[@"image"]]];

    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];

    [bgImageView setContentMode:UIViewContentModeScaleAspectFill];
    [bgImageView setClipsToBounds:YES];

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = CGRectMake(0, cell.bounds.size.height - cell.bounds.size.height / 2, cell.bounds.size.width, cell.bounds.size.height/2);
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    //gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:0.5], nil];
    [bgImageView.layer insertSublayer:gradient atIndex:0];

    cell.backgroundView = bgImageView;

    return cell;



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

    return 10; // This is the minimum inter item spacing, can be more



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

    return 10;



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;

    int x = screenWidth/2 - 15;
    int y = x;

    return CGSizeMake(x, y);



- (void)collectionView:(UICollectionView *)colView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath

    NSDictionary *article = self.articles[indexPath.row];
    NSURL *URL = [NSURL URLWithString:article[@"url"]];

    self.webViewController.title = article[@"title"];
    self.webViewController.URL = URL;
    [self.navigationController pushViewController:self.webViewController
                                         animated:YES];

【问题讨论】:

分享你的集合视图数据源方法实现 在dispatch_get_main_queue()下试试这个[_collectionView reloadData] 嗨,m.davlet,我对这个很陌生(这只是我的第一个应用程序)当你说集合视图数据源方法时你在问什么。你说的是 cellForItemAtIndexPath 方法吗? 我添加了我认为是 Collection View 数据源方法。如果我错了,请纠正我 【参考方案1】:

如果NSLog(@"%@", self.articles) 工作并显示您已证明您正在取回网络数据的数据。您是否正确地将 UIView 设置为委托和数据源?这通常在 UIViewController 类的顶部完成,如下所示:

class MyClassName: UICollectionViewDataSource 
   // Your code here.

检查数据源是否设置正确的一种方法是在此处设置断点

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

   print(“This shows that I’m getting called”)
   // Your custom code

当你打电话时

dispatch_async(dispatch_get_main_queue(), ^
    [self.collectionView reloadData];
)

这将依次调用 cellForItemAtIndexPath 来显示数据。如果 cellForItemAtIndexPath 没有被调用,那么你没有正确设置你的 UICollectionViewDataSource

【讨论】:

嗨,我在 CollectionViewCells 中显示了信息,当我在 cellForItemAtIndexPath 中放入消息时,它会被调用。我的问题是当我更新 JSON 并尝试拉动以刷新它时,它不会保留在相同的单元格中。如果我重置模拟器,那么它似乎会读取更新的 JSON,但我希望它在我拉刷新时更新。【参考方案2】:

试试这个

- (void)fetchFeed  
    NSString *requestString = @"http://www.jameslester.xyz/example.json"; 
    NSURL *url = [NSURL URLWithString:requestString];
    NSURLRequest*req = [NSURLRequest requestWithURL:url]; 

    NSURLSessionDataTask*dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)  
    NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
    self.articles = jsonObject[@"articles"]; 
    NSLog(@"%@", self.articles); 
    NSLog(@"Feed Fetched!!!"); 

        dispatch_async(dispatch_get_main_queue(), ^
            [_collectionView reloadData]; 
        ); 
    ]; 
    [dataTask resume]; 

【讨论】:

嗨 Ravi,我刚试过,但它似乎仍然没有更新

以上是关于来自 JSON 的数据未在 UICollectionView 中更新的主要内容,如果未能解决你的问题,请参考以下文章

未在 TableView 中重新加载正确的数据

更新的 json 数据未在 android 中显示

JSON数据未在swift3中使用Alamofire进行解析

json数据未在python中使用jquery加载

页面未在来自 iOS 的 URLRequest 上重定向

将数组保存到Big Query