3

我在我創建的測試項目中沒有使用Interface Builder的UICollectionView。當我運行應用程序時,通過數據源給集合視圖的測試視圖顯示在(0,0)周圍的右上角。我不能爲了我的生活找出原因。我曾嘗試向單元格的內容視圖添加約束條件。我也試圖搞亂項目插入委託功能,但這似乎沒有什麼區別。我錯過了什麼嗎?UICollectionView與UICollectionFlowLayout不在正確的位置顯示單元格

這裏是測試視圖控制器的代碼。

#import "TestViewViewController.h" 

@interface TestViewViewController() <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource> 

@property (strong, nonatomic) UICollectionView *collectionView; 
@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout; 
@property (strong, nonatomic) NSMutableArray *testViews; 

@end 

@implementation TestViewViewController 

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

- (void)loadView { 


    self.view = [[UIView alloc] init]; 
    self.view.backgroundColor = [UIColor whiteColor]; 

    self.flowLayout = [[UICollectionViewFlowLayout alloc] init]; 

    self.testViews = [[NSMutableArray alloc] init]; 

    UIView *testView = [[UIView alloc] init]; 
    testView.backgroundColor = [UIColor blueColor]; 
    testView.translatesAutoresizingMaskIntoConstraints = NO; 

    UILabel *testLabel = [[UILabel alloc] init]; 
    testLabel.translatesAutoresizingMaskIntoConstraints = NO; 
    testLabel.text = @"I hate collection views."; 

    [testView addSubview:testLabel]; 

    testView = [[UIView alloc] init]; 
    testView.backgroundColor = [UIColor redColor]; 
    testView.translatesAutoresizingMaskIntoConstraints = NO; 

    testLabel = [[UILabel alloc] init]; 
    testLabel.translatesAutoresizingMaskIntoConstraints = NO; 
    testLabel.text = @"I really do."; 

    [testView addSubview:testLabel]; 


    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout]; 
    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO; 
    self.collectionView.delegate = self; 
    self.collectionView.dataSource = self; 
    self.collectionView.backgroundColor = [UIColor grayColor]; 
    [self.view addSubview:self.collectionView]; 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"]; 


    NSDictionary *views = @{@"collectionView": self.collectionView}; 

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[collectionView]|" options:0 metrics:nil views:views]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[collectionView]|" options:0 metrics:nil views:views]]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

#pragma mark - UICollectionView Datasource 
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 
    return self.testViews.count; 
} 

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { 
    return 1; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 
    NSLog(@"%i", self.testViews.count); 
    [cell.contentView addSubview: self.testViews[indexPath.row]]; 

    return cell; 
} 

#pragma mark – UICollectionViewDelegateFlowLayout 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

// UIView *statView = self.testViews[indexPath.row]; 
    return CGSizeMake(100.0, 100.0); 
} 


@end 

回答

1

對於開放劑的CGRectZero宏是CGRectMake(0, 0, 0, 0)等效。看起來,如果要定義要繪製集合視圖的空間,並且0,0的原點將放在視圖的左上角,您會遇到麻煩。我試圖創建你的代碼的應用程序 - 最後得到的東西展現出來的時候,我得到了代碼爲loadView看起來像這樣:

- (void) loadView 
{ 
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
    self.testViews = [[NSMutableArray alloc] init]; 
    UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 80)]; 
    testView.backgroundColor = [UIColor blueColor]; 
    testView.translatesAutoresizingMaskIntoConstraints = NO; 

    UILabel* testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 80)]; 
    testLabel.translatesAutoresizingMaskIntoConstraints = NO; 
    testLabel.text = @"I hate collection views."; 

    [testView addSubview:testLabel]; 

    [self.testViews addObject:testView]; 

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, 320, 320) collectionViewLayout:self.flowLayout]; 
    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO; 
    self.collectionView.delegate = self; 
    self.collectionView.dataSource = self; 
    self.collectionView.backgroundColor = [UIColor grayColor]; 
    [self.view addSubview:self.collectionView]; 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"]; 
} 

區別在於對象用的幀大小創建 - 這觀點是添加到視圖等等。

我的想法是,你最好在XIB中創建你的UICollectionView - 設置所有參數,並創建一個UICollectionViewCell的子類,以便能夠根據需要對單元格內容做更多的事情。還有很多事情要做,以使其成爲可用的任意數量的單元格的UICollectionView,但我不得不說,UICollectionView不是一個容易處理的對象 - 所以我在學習曲線出現時同情你。

+0

我最終保留了CGRectZero,並且在sizeForItemAtIndexPath:方法中使用了[myView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] .height,並在cellForItemAtIndexPath:中使用NSLayoutConstraints將cell添加到cell.contentView中。儘管謝謝你的建議! –

相關問題