2015-01-27 61 views
0

我是新使用UICollectionView,因爲我一直在使用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; 
} 

​​

發生了什麼事與 「炸彈」 和 「.COM」 的細胞?爲什麼它切斷屏幕?

+0

你的收藏視圖的框架是什麼?你是怎麼設定的? – rdelmar 2015-01-27 00:11:16

回答

1

您需要在集合視圖上設置視圖約束。查看Autolayout編程爲UICollectionView設置約束。

+0

我怎麼知道如何設置UITableView的約束但不是UICollectionView,我不知道。真是個傻瓜! :) – Pangu 2015-01-27 02:14:36