【问题标题】:UICollectionViewContoller cell formatting and arrangementUICollectionViewContoller 单元格格式和排列
【发布时间】:2013-10-13 01:33:18
【问题描述】:

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

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"];

}

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

【问题讨论】:

  • 你的问题解决了吗?

标签: iphone ios objective-c ios7 uicollectionview


【解决方案1】:

用这个更新你的自定义单元格:

#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

【讨论】:

    【解决方案2】:

    如果您希望标签为两行,请将其 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多