【问题标题】:UICollectionViewCell does not take maximum widthUICollectionViewCell 不占用最大宽度
【发布时间】:2015-03-23 10:28:18
【问题描述】:

我试图让单元格尽可能宽,但我最终是这样的:

我尝试在 xcode 中删除边距、行距等:

但水平边距仍然很大,我想要应用相同的垂直边距。

有什么想法吗?

附上示例代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.delegate = self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 9;
}

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

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

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

【问题讨论】:

  • 您对此解决方案有何看法:stackoverflow.com/questions/13780138/…?
  • 您似乎要求单元格大小为 100 x 100?
  • @skyline75489 是的,我正在尝试覆盖它。我将它拖到视图中,它变成了 100x100

标签: ios objective-c cocoa-touch uicollectionview


【解决方案1】:

您将 Collection 单元格设置为 100x100,并设置为 3 at row。

所以 UICollectionView 最小为 300/300。如果您将视图设为 320(正常 iPhone 宽度),则 Collection View 会自动设置单元格之间的填充。

编辑:

试试:

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

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];

    //3 because you wanted 3 cells. 18 is the min padding between the cell.
    cell.frame.size.width = collectionView.frame.size.width/3-18;

    return cell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2016-08-11
    相关资源
    最近更新 更多