ios - UICollectionView 单元格与屏幕对齐吗?

标签 ios objective-c uicollectionview uicollectionviewcell

因为我一直在使用 UITableView,所以我是使用 UICollectionView 的新手,所以我先道歉。

无论如何,我有一些简单的代码,但由于某些我还不熟悉的原因,一些单元格被放置在屏幕外而不是被移到下一层。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    array = [ [NSMutableArray alloc] initWithObjects:@"Apple", @"Is", @"The", @"Bomb", @".com", @":D", nil];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.0;
}

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UILabel *label = (UILabel *)[cell viewWithTag:100];

    label.text = [array objectAtIndex:indexPath.row];

    [cell.layer setBorderWidth:2.0f];
    [cell.layer setBorderColor:[UIColor whiteColor].CGColor];

    [cell.layer setCornerRadius:10.f];

    return cell;
}

enter image description here

带有“Bomb”和“.com”的单元格发生了什么?为什么会截屏?

最佳答案

您需要在 Collection View 上设置 View 约束。查看 Autolayout 编程以设置 UICollectionView 的约束。

关于ios - UICollectionView 单元格与屏幕对齐吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28161127/

相关文章:

swift - 设备方向更改时如何更改 UiCollectionView 单元格大小?

iOS - 检测 Blow into Mic 并转换结果! ( swift )

ios - 一段时间后(1 分钟)检查用户仍在区域中

ios - NSUndoManager removeAllActionsWithTarget 崩溃

ios - iOS-compelationHandler:nil导致应用程序崩溃

ios - UICollectionView 初始滚动滞后?

ios - 接收器没有带标识符的 segue ...以编程方式创建的 segue

音乐开始播放的 iOS 通知

objective-c - 如何构建一个在 iOS 和 Web 上使用的数据库

ios - 是否可以将 UICollectionView 作为一个单元进行缩放?