2

我将我的应用程序设置为从数组中获取数据,并将其放入要在UICollectionViewController. 我有自定义单元格类,并更改了单元格大小和行数,但它没有正确显示。我想要 5 行,单元格中的标签有两行。

集合视图控制器.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"];

}

细胞.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

4

2 回答 2

0

如果您希望标签为两行,请将其numberOfLines属性设置为 2。您还应该将 设置为lineBreakModeNSLineBreakByWordWrapping这意味着如果一个单词超出单元格的边界,则该单词将被绘制在第二行。这是它的样子:

-(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;
}
于 2013-10-13T02:11:41.703 回答
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
于 2013-10-13T02:16:24.540 回答