我有一个CollectionView,这是我的主菜单。它工作得很好,但现在,我在菜单上有更多行,用户可以滚动它。
单元格包含一个图像,表示选项图标和一个包含菜单标题的标签。
在用户滚动或转到菜单并再次返回后,我的收藏视图的单元格会被弄乱"第一行,到最后一行,比,我再次滚动,标签消失......好吧,真的很混乱。
我在StackOverflow上看到了问题并尝试了它的每个解决方案。比如,使用[cell setNeedsDisplay]或在我的CustomCell.h上实现方法setFrame,但有时滚动后单元格仍然在乱码。
我的代码:
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 2;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 6;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"MenuOptionCell";
MyCustomCell *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(10, cell.frame.size.height-5, cell.frame.size.width-20, 1)];
seperatorView.backgroundColor = [UIColor colorWithRed:26.0/255.0 green:100.0/255.0 blue:119.0/255.0 alpha:1.0];
[cell.contentView addSubview:seperatorView];
UIView *seperatorView2 = [[UIView alloc] init];
NSInteger nsint = idxCollection;
if(idxCollection == 0)
seperatorView2 = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.size.width-1, 10, 1, cell.frame.size.height)];
else{
NSLog(@"%ld", (nsint % 2));
if((nsint % 2) == 0){
if(idxCollection != 8)
seperatorView2 = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.size.width-1, 0, 1, cell.frame.size.height)];
else
seperatorView2 = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.size.width-1, 0, 1, cell.frame.size.height-10)];
}
}
seperatorView2.backgroundColor = [UIColor colorWithRed:26.0/255.0 green:100.0/255.0 blue:119.0/255.0 alpha:1.0];
[cell.contentView addSubview:seperatorView2];
switch (idxCollection) {
case 0:
lbl.text = @"FIRST MENU";
img.image = [UIImage imageNamed:@"first_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 1:
lbl.text = @"SECOND MENU";
img.image = [UIImage imageNamed:@"second_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 2:
lbl.text = @"THIRD MENU";
img.image = [UIImage imageNamed:@"third_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 3:
lbl.text = @"FOURTH MENU";
img.image = [UIImage imageNamed:@"fourth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 4:
lbl.text = @"FIFTH MENU";
img.image = [UIImage imageNamed:@"fifth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 5:
lbl.text = @"SIXTH MENU";
img.image = [UIImage imageNamed:@"sixth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 6:
lbl.text = @"SEVENTH MENU";
img.image = [UIImage imageNamed:@"seventh_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 7:
lbl.text = @"EIGHTH MENU";
img.image = [UIImage imageNamed:@"eigthth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 8:
lbl.text = @"NINTH MENU";
img.image = [UIImage imageNamed:@"ninth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 9:
lbl.text = @"TENTH MENU";
[lbl setFont:[UIFont fontWithName:@"Helvetica" size:10.0f]];
img.image = [UIImage imageNamed:@"tenth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 10:
lbl.text = @"ELEVENTH MENU";
img.image = [UIImage imageNamed:@"eleventh_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
case 11:
lbl.text = @"TWELFTH MENU";
img.image = [UIImage imageNamed:@"twelfth_image.png"];
[img setFrame:CGRectMake(0,0,20,20)];
img.contentMode = UIViewContentModeScaleAspectFit;
break;
default:
break;
}
idxCollection++;
[cell setNeedsDisplay];
return cell;
}