【问题标题】:My UICollectionView does not call its datasource methods?我的 UICollectionView 没有调用它的数据源方法?
【发布时间】:2015-09-10 23:21:21
【问题描述】:

我刚刚有了一个标准的UIViewController 子类和一个UICollectionViewUICollection 视图没有调用它的数据源(或委托)方法,但我看不到哪一块丢失了。

这是呈现 UICollectionView 的 UIViewController 类的界面:

@interface PastViewController ()  <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
...
@property (nonatomic, weak) UICollectionView *collectionView;
@end

这是我的 viewDidLoad(几乎)的全部内容:

- (void)viewDidLoad {

    [super viewDidLoad];    


    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 100);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    //self.complaints is used to generate the collection view cells
    //the log always prints out 30 complaints
    self.complaints = [self.complaintDatabase getComplaints ];
    NSLog(@"got a total OF %d complaints", self.complaints.count);

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];


    [self.collectionView reloadData];
    [self.collectionView reloadInputViews];
}

我还将在最后发布数据源、委托和委托流布局协议。视图控制器中没有添加其他子视图,并且我已经设置了委托并声明了委托协议,因此以下 SO 帖子不适用:

我错过了什么?为什么永远不会调用数据源方法(和委托方法)?

以下是为各种协议实现的方法:

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    NSLog(@"should be seeing %d collection view cells", self.complaints.count);
    return self.complaints.count;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSLog(@"QUERYING NUMBER OF SECTIONS IN COLLECTION VIEW");
    return 1;
}


- (UICollectionViewCell  *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VideoCollectionViewCell *cell = [[VideoCollectionViewCell alloc] initWithCoder:@"cell"];
    cell.complaint = self.complaints[indexPath.row];
    return cell;
}


#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"calling didselctitem at path");
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"diddeselectitem");
}


#pragma mar - UICollectionViewFlowDelegateLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100, 100);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(50, 20, 50, 20);
}

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    @property (nonatomic, strong) UICollectionView *collectionView;

    weak更改为strong,否则collectionView将在赋值后被释放,这意味着collectionView将变为nil,这样就永远不会调用数据源方法。

    另一种方法:

    collectionView 保留为weak

    - (void)viewDidLoad {
    
        [super viewDidLoad];    
    
    
        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(100, 100);
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
        //self.complaints is used to generate the collection view cells
        //the log always prints out 30 complaints
        self.complaints = [self.complaintDatabase getComplaints ];
        NSLog(@"got a total OF %d complaints", self.complaints.count);
    
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.view addSubview:collectionView];
        self.collectionView = collectionView;
    
        [self.collectionView reloadData];
        [self.collectionView reloadInputViews];
    }
    

    见:Creating views programmatically Strong Vs Weak subviews in controller

    【讨论】:

    • 你不应该声明属性strong。而是使用新的集合视图对象创建一个变量,将其添加到控制器视图中,然后将其分配给属性。 more information
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2015-03-31
    相关资源
    最近更新 更多