2013-10-13 40 views
2

我有我的應用程序設置爲從數組中取數據,並將其放入要在UICollectionViewController中查看的單元格中。我有自定義單元類,並更改了單元格大小和行數,但沒有顯示正確。我想要5行,單元格中的標籤有兩行。UICollectionViewContoller單元格格式和排列

CollectionViewController.m

#import "CollectionViewController.h" 
#import "Cell.h" 

@interface CollectionViewController() 

@end 

@implementation CollectionViewController 

//Delegate Methods 

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

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

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    Cell * aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bingoCell1" forIndexPath:indexPath]; 
    aCell.cellContent.text = self.cellArray[indexPath.row]; 
    return aCell; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

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

    self.cellArray = 
    @[@"Type 1", @"Type 2", @"Type 3", @"Type 4", @"Type 5", @"Type 6", @"Type 7", @"Type 8", @"Type 9", @"Type 10", @"Type 11", @"Type 12", @"Type 13", @"Type 14", @"Type 15", @"Type 16", @"Type 17", @"Type 18", @"Type 19", @"Type 20", @"Type 21", @"Type 22", @"Type 23", @"Type 24", @"Type 25"]; 

} 

Cell.m

#import "Cell.h" 

@implementation Cell 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

結果表中的圖片可以在這裏找到: http://i.stack.imgur.com/qmOZp.png

+0

你解決問題了嗎? –

回答

0

更新與此您的自定義單元格:

#import "Cell.h" 


@implementation Cell 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.cellContent.numberOfLines = 2; 
     self.cellContent.lineBreakMode = NSLineBreakByWordWrapping; 
    } 
    return self; 
} 


-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     // Initialization code 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.cellContent.numberOfLines = 2; 
      self.cellContent.lineBreakMode = NSLineBreakByWordWrapping; 
     }); 
    } 
    return self; 
} 
/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 
0

如果你想要標籤是兩排,設置它的numberOfLines屬性設置爲2.您還應該將lineBreakMode設置爲NSLineBreakByWordWrapping,這意味着如果某個單詞超出了單元格的範圍,則該單詞將被繪製在第二行上。下面是它會是什麼樣子:

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    Cell * aCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bingoCell1" forIndexPath:indexPath]; 
    aCell.cellContent.numberOfLines = 2; 
    aCell.cellContent.lineBreakMode = NSLineBreakByWordWrapping; 
    aCell.cellContent.text = self.cellArray[indexPath.row]; 
    return aCell; 
}