【发布时间】:2014-12-23 15:15:30
【问题描述】:
我有一个 UICollectionView,我试图将其用作矩阵,其中第一个“列”用作行的名称(颜色),然后在其他项目中放入我要购买的商品数量。我不得不将它分成两个集合,因为标题必须固定在那个位置。
我的问题是最初的情况是这样的:
然后,当我稍微滚动一下灰色集合并使用它时......
应该管理该部分的代码是这样的:
- (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;
}
我做错了什么?
编辑: 我让程序在同一个单元格上写不同的标签只是为了表明索引路径是错误的。如果它们是正确的,它只会在相同的位置用相同的文本重新添加标签(我知道这仍然是错误的,但它只是为了调试)
【问题讨论】:
标签: objective-c xcode uicollectionview