0

I have a UICollectionView that I'm trying to use as a matrix with a first "column" used as the name of the row (colors) and then in the other items I put how many goods I'm buying. I had to split it in two collections because the header has to be fixed in that position.

My problem is that the initial situation is this: Correct situation

Then, when I scroll the gray collection a bit and play with it...

enter image description here

The code which should manage the part is this:

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

CGSize cellSize = cell.frame.size;
int item = (int)indexPath.item;
int section = (int)indexPath.section;
NSLog(@"S:%d I:%d",section,item);

/*NSMutableArray *array = [matrice objectAtIndex:section];

if([array objectAtIndex:row] != [NSNull null]){
    cell = [[matrice objectAtIndex:section] objectAtIndex:10-row];
    return cell;
}else{
    [[matrice objectAtIndex:section] replaceObjectAtIndex:row withObject:cell];
}*/

if(item == 0){ //Colori
    int labelWidth = cellSize.width - 5;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((cellSize.width/2)-labelWidth/2, (cellSize.height/2)-labelWidth/2, labelWidth, labelWidth)];
    Colore *colore = [cartellaColore.arrayColori objectAtIndex:section];
    [label setText:colore.descrizione];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
    [cell addSubview:label];

    [cell setBackgroundColor:[UIColor whiteColor]];
}
else{
    [cell setBackgroundColor:[UIColor grayColor]];
}

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2*section, 2*item, 35, 10)];
[label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:8]];
[cell addSubview:label];


return cell;

}

What am I doing wrong?

EDIT: I let the program write different label on the same cell just for showing that the indexpaths are wrong. If they were correct it would just re-add the label in the same position with the same text (which I know is still wrong but it was just for debugging)

Alberto Schiariti
  • 1,546
  • 2
  • 15
  • 31

2 Answers2

1

You can't add a label like that in your cellForRowAtIndexPath, you have to subclass a UICollectionViewCell.

I made a response to this kind of issue here : UICollectionView adding image to a cell

Just apply what I explained to a UILabel instead of a UIImageView, and your issues should disappear.

Let me know though.

Community
  • 1
  • 1
Kujey
  • 1,122
  • 6
  • 17
  • As I wrote down here: My fault: the commented part is actually not commented and it was for avoiding that error but that still mix up the things. And as you can see, if the indexpath were correct, I'd just have a label over a label, but in the same place and with the same text. So the error should be somewhere else. – Alberto Schiariti Dec 23 '14 at 15:39
  • I don't know what hides behind your matrice array, but still i strongly advise you to do it the way i described. – Kujey Dec 23 '14 at 15:50
  • If you need some help to understand, i can write you down some of the code you will need. – Kujey Dec 23 '14 at 15:53
  • I'll let you know. Thanks in advance. – Alberto Schiariti Dec 23 '14 at 15:57
  • I don't really know why in my it got all screwed up but doing your way seems working fine. Thanks :-) – Alberto Schiariti Dec 24 '14 at 08:09
0

This is your mistake:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2*section, 2*item, 35, 10)];
[label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:8]];
**[cell addSubview:label];**

you are stacking labels in cells, the cell are reusable, and each time you dequeue a reusable cell you add a label to it so if the dequeued cell has already been presented you will add another label to it and ect. You should protect your self from that by checking if that cell already has label and if it has than just change its text property, and if it doesn't do the code above... One way to do it is to add a specific tag to the label and then when you dequeue a cell just check if it has a view with that tag, if it has cast the view to label and change its text property so:

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

    if([cell viewWithTag:111]){
        UILabel *label = (UILabe*)[cell viewWithTag:111];
        [label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
    }
    else{
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2*section, 2*item, 35, 10)];
        [label setText:[NSString stringWithFormat:@"S:%d I:%d",section,item]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:8]];
        [label setTag:111];
        [cell addSubview:label];
    }

...

AntonijoDev
  • 1,317
  • 14
  • 29
  • My fault: the commented part is actually not commented and it was for avoiding that error but that still mix up the things. – Alberto Schiariti Dec 23 '14 at 15:39
  • But why do you want to handle cell dequeue, the collectionView handles it internaly, that is why you have it on the top of cell for row, you only need to handle the right data on specified indexPath that method also provides for you, I guess that messing with dequeue beats the purpose of cell reusibility – AntonijoDev Dec 23 '14 at 15:43
  • I didn't want to mess it up, that's why I'm asking here why it doesn't work even if I comment that part. Shouldn't I only have some labels that "overwrite" other labels with this code? Because instead I have labels that write different things in the same cell. – Alberto Schiariti Dec 23 '14 at 15:49
  • No they will just go on top of the old ones, and they are transparent so you'll see the text of the older labels beneeth them also, like your picture shows. indexPath is your matrix navigator, so just find data on row/section coordinates and fill your dequeued cell with that data, and protect yourselfe from stacking labels like I showed you and you will be alright.. – AntonijoDev Dec 23 '14 at 16:04
  • Yeah, but if the indexpath were the same, I should have the same text in same coordinates. So, even if that's trasparent, I should see just one label over another. – Alberto Schiariti Dec 23 '14 at 16:06