1

I have a collectionview that will be a menu with two columns, without spacing: so each cell with width 160. And I need a line separating each cell.

Each cell contains an imageview and a label.

I'm trying to add a line separator between the cells like this: How to add views between UICollectionViewCells in a UICollectionView?

But the result is: menu

Code:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return 2;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
     return [options count];
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath  *)indexPath {
    static NSString *identifier = @"MenuOptionCell";

    UICollectionViewCell *cell = [self.menu dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    UIImageView *img = (UIImageView*) [cell viewWithTag:100];
    UILabel *lbl = (UILabel*) [cell viewWithTag:90];

    UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+cell.frame.size.height, cell.frame.size.width-10, 1)];
    seperatorView.backgroundColor = [UIColor whiteColor];
    [self.menu addSubview:seperatorView];
    [self.menu bringSubviewToFront:seperatorView];



    UIView *seperatorView2 = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+cell.frame.size.width-10, cell.frame.origin.y, 1, cell.frame.size.height)];
    seperatorView.backgroundColor = [UIColor whiteColor];
    [self.menu addSubview:seperatorView2];
    [self.menu bringSubviewToFront:seperatorView2];

 lbl.text = @"TEST";
 img.image = [UIImage imageNamed:@"test.png"];
return cell;
}
Community
  • 1
  • 1
Lücks
  • 3,806
  • 2
  • 40
  • 54
  • "How can I fix these problems?" By writing different code, perhaps. But since you have not shown what code you _are_ writing, how can anyone help you? – matt Jan 16 '15 at 18:35
  • @matt I added more details – Lücks Jan 16 '15 at 18:41
  • Your width says 130... not 160? – Mike Welsh Jan 16 '15 at 18:58
  • @MikeWelsh Added new printscreens, with 160width – Lücks Jan 16 '15 at 19:05
  • Did you set a layout for the collection view? e.g. `UICollectionViewFlowLayout`. If so, `minimumInteritemSpacing` defaults to 10 and you probably want 0 – Mike Welsh Jan 16 '15 at 19:29
  • @MikeWelsh the problem is that the separator line it's not appearing. – Lücks Jan 16 '15 at 19:34
  • Instead of adding the separator to the `menu` view, I think you should instead add it to the `cell.contentView`. That way, the separator is part of the cell and will also move with the cell. I suspect that with what you're doing, the cells always get brought to the front and cover up the separator you're adding. – Mike Welsh Jan 16 '15 at 19:39
  • I changed to [cell.contentView addSubview:seperatorView]; but it's not appearing anymore :( – Lücks Jan 16 '15 at 19:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68986/discussion-between-mike-welsh-and-lucks). – Mike Welsh Jan 16 '15 at 20:18

0 Answers0