2016-05-09 4 views
3

Как удалить интервал в ячейках коллекции. Я хочу получить 18 строк и 22 столбца, которые должны соответствовать по высоте и ширине кадра. Я переопределяю UICollectionViewFlowLayout, но после последнего столбца в представлении коллекции есть пробел. Может кто-нибудь, пожалуйста, помогите мне решить эту проблему.Интервал в ячейках CollectionView

+0

Высота независим здесь. Для ширины вам нужно управлять им. Дайте правильную ширину и пространство в соответствии с вашим требованием. –

+0

я принять ширину экрана и разделить его на 22. - (CGSize) CollectionView: (UICollectionView *) CollectionView макет: (UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *) indexPath {плавать cellWidth = screenWidth/[self.advncdObj .motionGridX integerValue]; CGSize size = CGSizeMake (cellWidth, cellHeight);} – user1728987

ответ

0

UICollectionViewFlowLayoutDelegate метод поможет вам ..

Вариант 1:

- (CGFloat)collectionView:(UICollectionView *)collectionView layout: (UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section 
{ 
    return 0; // This is the minimum inter item spacing, can be more 
} 

Вариант 2:

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; 
flow.itemSize = CGSizeMake(cellWidth, cellHeight); 
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal; 
flow.minimumInteritemSpacing = 0; 
flow.minimumLineSpacing = 0; 
[mainCollectionView reloadData]; 
mainCollectionView.collectionViewLayout = flow; 

Последняя строка важно, где мы присваивающей макет

, если вам нужно больше места, чтобы организовать ваши клетки отрегулируйте edgeInsets also.and также вы можете обратиться по этой ссылке iOS - UICollectionView spacing still there when set to 0 - How to set with no spacing between cells

+0

Мне не нужен промежуток между ячейками. – user1728987

+0

Просто попробуйте с помощью 'return 0' в этом методе –

1

Есть два способа сделать это ----

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

#pragma mark - Collection view 
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 30; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; 

    mCell.backgroundColor = [UIColor lightGrayColor]; 

    return mCell; 
} 

#pragma mark Collection view layout things 
// Layout: Set cell size 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row); 
    CGSize mElementSize = CGSizeMake(104, 104); 
    return mElementSize; 
} 
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
    return 0.0; 
} 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { 
    return 2.0; 
} 

// Layout: Set Edges 
- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 
    // return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right 
    return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right 
} 

@end 

И наоборот. --------> 1. Стандартный формат потока. 2.Add реализация так:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray *answer = [super layoutAttributesForElementsInRect:rect]; 

    for(int i = 1; i < [answer count]; ++i) { 
     UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i]; 
     UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1]; 
     NSInteger maximumSpacing = 0; 
     NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame); 

     if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) { 
      CGRect frame = currentLayoutAttributes.frame; 
      frame.origin.x = origin + maximumSpacing; 
      currentLayoutAttributes.frame = frame; 
     } 
    } 
    return answer; 
} 
+0

Я переопределяю и использую описанный выше метод, но в этом случае представление коллекции не подходит для ширины устройства, а небольшое пространство после просмотра коллекции – user1728987

+1

в виде раскадровки - просмотр коллекции, есть вы выбрали минимальный интервал для ячеек как 0.0 ?? – raj

+0

Я добавил просмотр коллекции программно – user1728987

0
#pragma mark Collection View Delegate 
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 330; 
} 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(Screen_Width/22, Screen_Width/22); 
} 
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{ 
    return UIEdgeInsetsMake(0, 0,0, 0); 
} 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    customcell *cell = (customcell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"customcell" forIndexPath:indexPath]; 

      if (cell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
      } 
    return cell; 
} 
+0

Попробуйте это. Это решение может помочь –

Смежные вопросы