UICollectionViewFlowLayout的严重错误:水平滚动模式下的minimumInteritemSpacing和minimumLineSpacing

时间:2014-11-06 11:07:32

标签: ios objective-c uicollectionview uicollectionviewlayout uicollectionviewdelegate

我在水平滚动模式UICollectionViewiOS 8中遇到了iOS 7的以下错误,这是我测试过的唯一错误。

我的问题

我想就此错误以及如何优雅地解决这个问题发表看法。

设置

UICollectionViewFlowLayout * layout ;
layout = [[UICollectionViewFlowLayout alloc] init] ;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal ;
layout.minimumInteritemSpacing = 5 ;
layout.minimumLineSpacing = 100 ;

错误

很难解释,但我会尽力而为。 当UICollectionView中的单元格大小不同时会发生错误。

  • 当所有单元格具有相同的大小时,它看起来像这样 enter image description here

  • 但是只要其中一个单元格的大小与其他单元格不同,它就会像这样 enter image description here

因此,它似乎与minimumInteritemSpacingminimumLineSpacing

互换

链接到最小的非工作示例

https://github.com/colasjojo/TEST_COLLECTION_VIEW_BUG

更多代码

#import "ViewController.h"
#import "MyCell.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *viewForCollectionView;
@property (nonatomic, assign, readwrite) NSInteger selectedIndex ;
@end

@implementation ViewController


//
//
/**************************************/
#pragma mark - Init
/**************************************/

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder] ;
    if (self)
    {
        [self configure] ;
    }
    return self ;
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] ;
    if (self)
    {
        [self configure] ;
    }
    return self ;
}


- (void)configure
{
    _selectedIndex = -1 ;
}




//
//
/**************************************/
#pragma mark - Life cycle
/**************************************/


- (void)viewDidLoad {
    [super viewDidLoad];


    UICollectionViewFlowLayout * layout ;
    layout = [[UICollectionViewFlowLayout alloc] init] ;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal ;
    layout.minimumInteritemSpacing = 5 ;
    layout.minimumLineSpacing = 100 ;



    UICollectionView * collectionView ;
    CGRect frame ;
    frame.size = self.viewForCollectionView.frame.size ;
    collectionView = [[UICollectionView alloc] initWithFrame:frame
                                        collectionViewLayout:layout] ;
    collectionView.dataSource = self ;
    collectionView.delegate = self ;
    collectionView.backgroundColor = [UIColor clearColor] ;

    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MyCell class])
                                               bundle:nil]
     forCellWithReuseIdentifier:@"MyCell"] ;



    [self.viewForCollectionView addSubview:collectionView] ;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//
//
/**************************************/
#pragma mark - Datasourcing
/**************************************/


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100 ;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell * cell ;
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell"
                                                     forIndexPath:indexPath] ;

    cell.label.text = [@([indexPath indexAtPosition:1]) stringValue] ; ;
    return cell ;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath indexAtPosition:1] == self.selectedIndex)
    {
        return CGSizeMake(200, 200) ;
    }
    else
    {
        return  CGSizeMake(110, 110) ;
    }
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * indexes = [NSMutableArray new] ;
    if (self.selectedIndex >= 0)
    {
        [indexes addObject:[NSIndexPath indexPathForItem:self.selectedIndex inSection:0]] ;
    }
    if (self.selectedIndex != [indexPath indexAtPosition:1])
    {
        [indexes addObject:indexPath] ;
        self.selectedIndex = [indexPath indexAtPosition:1] ;
    }
    else
    {
        self.selectedIndex = -1 ;
    }

    [collectionView reloadItemsAtIndexPaths:indexes] ;
}


@end

0 个答案:

没有答案