Objective c UICollectionView选择2个项目,它应该是一个
我试图在同一UIViewController中使用UITableView实现UICollectionView,但我遇到了一个问题,即每当我选择一个项目时,它都会选择另一个项目,这不应该是正确的 我将项目编号保存在NSUserDefault中,并在Objective c UICollectionView选择2个项目,它应该是一个,objective-c,ios7,uicollectionview,Objective C,Ios7,Uicollectionview,我试图在同一UIViewController中使用UITableView实现UICollectionView,但我遇到了一个问题,即每当我选择一个项目时,它都会选择另一个项目,这不应该是正确的 我将项目编号保存在NSUserDefault中,并在-(BOOL)viewwillbeen中检索它,以便获取选定的单元格索引 我试图跟踪并记录代码,但找不到问题。似乎一切正常,并更改了选定单元格的背景,取消选择了以前选定的单元格,但我发现另一个单元格的背景已更改,这是不应该发生的 代码如下: -(UICo
-(BOOL)viewwillbeen
中检索它,以便获取选定的单元格索引
我试图跟踪并记录代码,但找不到问题。似乎一切正常,并更改了选定单元格的背景,取消选择了以前选定的单元格,但我发现另一个单元格的背景已更改,这是不应该发生的
代码如下:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = (Cell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
// collection_flag : if the datasource array is zero it should be true
if ((collection_rows == 1 && collection_flag) || (indexPath.item == collection_rows) || (indexPath.item == collection_rows-1)) {
cell.semester.text = @"Add";
}else{
NSDictionary *temp = [semesters objectAtIndex:indexPath.item];
cell.semester.text = [temp objectForKey:@"SEMESTER_NUMBER"];
}
cell.layer.cornerRadius = 50;
cell.layer.borderColor = [UIColor redColor].CGColor;
cell.layer.borderWidth = 1;
//changing the selected cell background on appreaing of uicollectionview
if (indexPath.item == prevCell.item) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//prevCell is the selected cell indexPath
if (prevCell == nil) {
prevCell = indexPath;
[[NSUserDefaults standardUserDefaults] setInteger:prevCell.item forKey:@"selected cell"];
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"did select cell"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"prevCell didnt exist and now created");
}else{
NSLog(@"prevCell item is %d and indexPath %d",prevCell.item , indexPath.item);
Cell *selectedCell = (Cell *)[collection cellForItemAtIndexPath:prevCell];
selectedCell.backgroundColor = [UIColor clearColor];
prevCell = indexPath;
NSLog(@"new prevCell item is %d",prevCell.item);
[[NSUserDefaults standardUserDefaults] setInteger:prevCell.item forKey:@"selected cell"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Cell *cell = (Cell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
if (indexPath.item < collection_rows-1) {
NSString *semester_number = cell.semester.text;
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM COURSES WHERE SEMESTER_NUM = '%@'",semester_number];
semester_details = [DataBase loadFromDataBase:sql];
if ([semester_details count] > 0 && !isTableVisible) {
[UIView animateWithDuration:0.75 animations:^{
tabel.alpha = 1;
}];
[tabel reloadData];
}else if ([semester_details count] > 0 && isTableVisible)
{
[tabel reloadData];
}else{
[UIView animateWithDuration:0.75 animations:^{
tabel.alpha = 0;
}];
}
}else{
Semesters *semester = [self.storyboard instantiateViewControllerWithIdentifier:@"semesterView"];
[self.navigationController pushViewController:semester animated:YES];
}
}
如果您的唯一目的是更改选定的单元格背景,并在选择另一个单元格时将其更改回以前的状态,那么您的操作是错误的。应使用委托方法查找选定/取消选定的单元格
// did Select
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
}
// did deselect
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
}
然后,您应该从委托方法中删除以下代码
if (prevCell == nil) {
prevCell = indexPath;
[[NSUserDefaults standardUserDefaults] setInteger:prevCell.item forKey:@"selected cell"];
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"did select cell"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"prevCell didnt exist and now created");
}else{
NSLog(@"prevCell item is %d and indexPath %d",prevCell.item , indexPath.item);
Cell *selectedCell = (Cell *)[collection cellForItemAtIndexPath:prevCell];
selectedCell.backgroundColor = [UIColor clearColor];
prevCell = indexPath;
NSLog(@"new prevCell item is %d",prevCell.item);
[[NSUserDefaults standardUserDefaults] setInteger:prevCell.item forKey:@"selected cell"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Cell *cell = (Cell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
及
我实现了委托,但无论何时我选择一个项目,它都会选择另一个项目。例如,当我选择项目0时,它将其背景更改为红色,并且项目5我按照您的建议执行了操作,但仍然存在相同的问题,我从委托方法中删除了代码,但没有解决我的问题UICollectionView就是这样工作的,您所描述的不是预期的行为,它可能来自你的其他代码,我看不到。也可以尝试清理(shift+alt+cmd+K)你的项目并从模拟器中删除应用程序,然后再试一次。很抱歉响应太晚,但我尝试了你的解决方案没有任何更改我发布了更多代码请检查是否有错误请告诉我
if (prevCell == nil) {
prevCell = indexPath;
[[NSUserDefaults standardUserDefaults] setInteger:prevCell.item forKey:@"selected cell"];
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"did select cell"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"prevCell didnt exist and now created");
}else{
NSLog(@"prevCell item is %d and indexPath %d",prevCell.item , indexPath.item);
Cell *selectedCell = (Cell *)[collection cellForItemAtIndexPath:prevCell];
selectedCell.backgroundColor = [UIColor clearColor];
prevCell = indexPath;
NSLog(@"new prevCell item is %d",prevCell.item);
[[NSUserDefaults standardUserDefaults] setInteger:prevCell.item forKey:@"selected cell"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Cell *cell = (Cell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
//changing the selected cell background on appreaing of uicollectionview
if (indexPath.item == prevCell.item) {
cell.backgroundColor = [UIColor redColor];
}