我在使用UICollectionView
时遇到的经验很少,但我设法创建了一个可滚动的界面,可显示100个级别的100个按钮,4个横向,25个向下(嗯,他们得到如你所知,即时生成)。级别的类型称为Tortoise
,因此您想知道以后的地狱是什么。对于Tortoise
,只有20个级别。
现在,我用来显示数字的单元格数据字符串放在单元格的常规背景上(表示该级别已解锁但未完成)。
我还有2张其他想要用作背景图片的图片(一张是锁定图片,其中没有数字字符串出现,另一张是与上面相同的背景,只需要一个小的勾选标记即可完成(以及顶部的数字字符串))。
为了向您通报,我使用Core Data来跟踪一组对象,以及是否锁定或解锁了一个级别。我调用一个名为figureOutLocks
的方法(就像我输入我的Tortoise UICollectionView一样),该方法在此数组NSNumber
中存储0,1或2 self.lockArray
个对象。这是方法:
- (void)figureOutLocks {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext* managedObjectContext = [(AppDelegate*)[[UIApplication sharedApplication]
delegate] managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Score" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entityDescription];
for (int i = 0; i < 20; i++) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %d AND %K == %d",
@"level", i, @"typeOfLevel", 0];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
Score* lockInfo = [[managedObjectContext executeFetchRequest:fetchRequest
error:&error] lastObject];
lockInfo.levelCompleted = [lockInfo valueForKey:@"levelCompleted"];
lockInfo.lockedLevel = [lockInfo valueForKey:@"lockedLevel"];
NSInteger complete = [lockInfo.levelCompleted integerValue];
NSInteger locked = [lockInfo.lockedLevel integerValue];
if ((locked == 0) && (complete == 0)) {
// level is unlocked but not complete (does not have any saved scores)
// lockArray gets a 0
[self.lockArray addObject:[NSNumber numberWithInteger:0]];
} else if ((locked == 1) && (complete == 0)) {
// level is locked which implies it is not complete
// lockArray gets a 1
[self.lockArray addObject:[NSNumber numberWithInteger:1]];
} else if ((locked == 0) && (complete == 1)) {
// level is complete thus it is unlocked
// lockArray gets a 2
[self.lockArray addObject:[NSNumber numberWithInteger:2]];
}
}
}
再次向您通报,第一级解锁但未完成,其他级别被锁定(因此未完成)。
另外,我创建了一个包含字符串对象1-20的NSArray *dataArray
和一个包含NSArray *compareArray
个对象1-20的NSNumber
。我的lockArray
是NSMutableArray
。
此外,我决定制作2个单独的UICollectionViewCell
子类,以便同时使用常规背景和锁定背景。我没有添加完成背景子类,因为我想确保锁定的背景有效。
这是主要方法:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *regular;
static NSString *cellIdentifier = @"tortoiseCell";
static NSString *tortIdentifier = @"tortoiseLocked";
TortoiseCell *cell = (TortoiseCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
TortoiseLocked *lockCell = (TortoiseLocked *)[collectionView dequeueReusableCellWithReuseIdentifier:tortIdentifier forIndexPath:indexPath];
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
NSMutableArray *locks = [self.compareArray objectAtIndex:indexPath.section];
NSNumber *locksData = [locks objectAtIndex:indexPath.row];
NSInteger locked = [locksData integerValue];
NSInteger lock = [[self.lockArray objectAtIndex:locked] integerValue];
if (lock == 0) {
[cell.buttonClick setTag:indexPath.row];
[cell.buttonClick setTitle:cellData forState:UIControlStateNormal];
[cell.buttonClick setBackgroundImage:[UIImage imageNamed:@"TortoiseLevels.png"]
forState:UIControlStateNormal];
[cell.buttonClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cell.buttonClick.titleLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:25]];
[cell.buttonClick addTarget:self action:@selector(buttonPressedSoWhatNumber:)
forControlEvents:UIControlEventTouchUpInside];
cell.buttonClick.layer.cornerRadius = 8;
cell.buttonClick.layer.masksToBounds = YES;
[cell addSubview:cell.buttonClick];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
regular = cell;
} else if (lock == 1) {
[lockCell.tortoiseLock setTag:indexPath.row];
[lockCell.tortoiseLock setBackgroundImage:[UIImage imageNamed:@"TortoiseLock.png"]
forState:UIControlStateNormal];
lockCell.tortoiseLock.layer.cornerRadius = 8;
lockCell.tortoiseLock.layer.masksToBounds = YES;
[lockCell addSubview:lockCell.tortoiseLock];
lockCell.layer.shouldRasterize = YES;
lockCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
regular = lockCell;
}
return regular;
}
我甚至做过什么?如果是这样,我该如何工作呢?我尝试使用一个UICollectionViewCell
子类,只是以编程方式更改背景,但这不起作用,这就是为什么你看到你用更大的if
语句看到的内容。有什么想法吗?
答案 0 :(得分:2)
您绝对不需要将两个单元格从表格中取出 - 您只会使用其中一个单元格。您的代码应该更像这样:
static NSString *cellIdentifier = @"tortoiseCell";
static NSString *tortIdentifier = @"tortoiseLocked";
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *regular;
NSMutableArray *locks = [self.compareArray objectAtIndex:indexPath.section];
NSNumber *locksData = [locks objectAtIndex:indexPath.row];
NSInteger locked = [locksData integerValue];
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
NSInteger lock = [[self.lockArray objectAtIndex:locked] integerValue];
if (lock == 0) {
TortoiseCell *cell = (TortoiseCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[cell.buttonClick setTag:indexPath.row];
[cell.buttonClick setTitle:cellData forState:UIControlStateNormal];
[cell.buttonClick setBackgroundImage:[UIImage imageNamed:@"TortoiseLevels.png"]
forState:UIControlStateNormal];
[cell.buttonClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cell.buttonClick.titleLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:25]];
[cell.buttonClick addTarget:self action:@selector(buttonPressedSoWhatNumber:)
forControlEvents:UIControlEventTouchUpInside];
cell.buttonClick.layer.cornerRadius = 8;
cell.buttonClick.layer.masksToBounds = YES;
[cell addSubview:cell.buttonClick];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
regular = cell;
} else if (lock == 1) {
TortoiseLocked *lockCell = (TortoiseLocked *)[collectionView dequeueReusableCellWithReuseIdentifier:tortIdentifier forIndexPath:indexPath];
[lockCell.tortoiseLock setTag:indexPath.row];
[lockCell.tortoiseLock setBackgroundImage:[UIImage imageNamed:@"TortoiseLock.png"]
forState:UIControlStateNormal];
lockCell.tortoiseLock.layer.cornerRadius = 8;
lockCell.tortoiseLock.layer.masksToBounds = YES;
[lockCell addSubview:lockCell.tortoiseLock];
lockCell.layer.shouldRasterize = YES;
lockCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
regular = lockCell;
}
return regular;
}
此外,由于您已经在故事板上设置了两种类型的单元原型,为什么还要以编程方式设置每个单元?只需让它们在故事板上看起来如何,并在代码中添加级别编号。