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中。尽管谢谢你的建议! –

相关问题