UICollectionView单元格在屏幕上对齐?

时间:2015-01-27 00:08:17

标签: ios objective-c uicollectionview uicollectionviewcell

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

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

- (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"?为什么会切断屏幕?

1 个答案:

答案 0 :(得分:1)

您需要在集合视图上设置视图约束。查看Autolayout编程以设置UICollectionView的约束。