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:
Then, when I scroll the gray collection a bit and play with it...
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)