【问题标题】:objective-c collectionview does not load dataobjective-c collectionview 不加载数据
【发布时间】:2013-01-10 23:49:10
【问题描述】:

不知何故,我的 collectionView 不加载我的数据。我从 facebook 获取数据并在填充数组后调用 reloadData,但没有任何反应......

不显示单元格,因为调用 ViewDidLoad 时数组为 0。

如何重新加载它在collectionView上生效? 我的代码有什么问题?

CollectionViewController.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>

@interface CollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *searches;
- (IBAction)getFotos:(id)sender; 
@end

CollectionViewController.m

#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "BHPhoto.h"

@interface CollectionViewController () 

@end

@implementation CollectionViewController

@synthesize collectionView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *searches = [[NSMutableArray alloc] init];
    self.searches = searches;

    [self getFotos:nil];
    NSLog(@"searches amount: %u", [self.searches count]);

    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collcell"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    int ret = [self.searches count];   // <-- here the searches return 0 instead of the filled searches size, what I though will happen if reloadData is called
    NSLog(@"numberOfItemsinSection - I: %d", ret); 
    return ret;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    int ret = 1;
    NSLog(@"numberOfSection - S: %d", ret);
    return ret;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"collcell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    return cell; 
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // @todo select
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    // @todo de-select
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
    return CGSizeMake(80, 80);
}

#pragma mark -
#pragma mark get Facebook fotos around me
- (IBAction)getFotos:(id)sender {
    NSLog(@"getFotos in collview");
    [FBRequestConnection
     startWithGraphPath:@"search?type=place&center=43.119340,22.694992&distance=1000&fields=picture"
     completionHandler:^(FBRequestConnection *connection,
                         id result,
                         NSError *error) {
         if (!error) {
              [self readjson:result];
         }
     }];
}

#pragma mark read json for search location
- (IBAction)readjson:(NSDictionary *)jsondata {

    NSLog(@"keys: %@", [jsondata allKeys]);
    for (id item in [jsondata objectForKey:@"data"]) {        

        // add Photos to array
        NSURL *photoURL = item[@"picture"][@"data"][@"url"]; 
        BHPhoto *photo = [BHPhoto photoWithImageURL:photoURL];
        [self.searches addObject:photo];

    }
    NSLog(@"searches content: %@", self.searches);
    [self.collectionView reloadData]; // <-- reload data is called but does not have an effect...

}


@end

调试输出:

2013-01-11 00:23:46.308 foobar[3099:19a03] getFotos in collview
2013-01-11 00:23:46.309 foobar[3099:19a03] searches amount: 0
2013-01-11 00:23:46.312 foobar[3099:19a03] numberOfSection - S: 1
2013-01-11 00:23:46.312 foobar[3099:19a03] numberOfItemsinSection - I: 0
2013-01-11 00:23:46.591 foobar[3099:19a03] keys: (
    data,
    paging
)
2013-01-11 00:23:46.592 foobar[3099:19a03] searches content: (
    "<BHPhoto: 0x9351460>",
    "<BHPhoto: 0x938ad20>",
    "<BHPhoto: 0x938ade0>",
    "<BHPhoto: 0x938ae90>",
    "<BHPhoto: 0x938af40>",
    "<BHPhoto: 0x938b000>"
)

干杯

【问题讨论】:

  • 我认为您正在 viewDidLoad 中调用 [self getFotos:nil]。在集合视图渲染和表视图渲染等方面可能存在时间问题。尝试通过另一个按钮强制调用 [self getFotos:nil] 或在 viewDidAppear 中执行此操作。根据您所展示的 NSLog 语句,在调用 getFotos 之后甚至不会调用 tableview 方法。
  • 我会尝试,但是通过 reloaddata 数据应该被重新加载,当它被调用时。此外,如果我对项目进行硬编码,以便显示项目,至少是占位符,reloaddata 无效。顺便提一句。它是一个collectionview,而不是tableview。干杯
  • 你的 CollectionViewController,是不是 UICollectionViewController 的子类。如果是,那么您是否明确添加了 Collectionview。在这种情况下,您不需要显式添加集合视图。可以发一下这个类的.h文件吗?
  • 是的,是 UICollectionViewController 的子类。 .h 文件将在我的 macbook 恢复后立即出现。
  • 在最初的帖子中添加了 .h 文件。

标签: ios objective-c reloaddata collectionview


【解决方案1】:

您忘记将您的类设置为它自己的委托/数据源。在你的控制器类初始化中,像这样添加这些行,这样它就知道你的协议实现是为自己的:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionview.delegate = self;
        self.collectionview.datasource = self;
    }
    return self;
}

这也花了我一段时间,但我注意到您的调试输出仅调用 numberOfItemsInSection: 一次(在 viewDidLoad 上)。当您调用 [self.collectionview reloadData] 时,它会再次调用该方法,因此您应该会看到另一条具有正确计数的调试消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2015-12-04
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多