UICollectionView选择2个项目,它应该是一个

时间:2014-08-06 20:34:32

标签: objective-c ios7 uicollectionview

我正在尝试在同一个UIViewController中使用UITableView实现一个UICollectionView,但我遇到了一个问题,每当我选择一个项目时,它选择另一个项目,这应该不对。

我在NSUserDefault中保存了项目编号并在-(BOOL)ViewWillAppear中检索它,这样我就可以获得所选的单元格IndexPath

我试图跟踪并记录代码,但无法找到问题,看起来一切正常并更改所选单元格的背景并取消选择上一个选定的单元格,但我发现另一个单元格的背景已更改,但它不应该不会发生。

这是代码:

-(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];
    }

}

更新:我在这里发布-viewDidLoad和其他一些方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    semesters = [DataBase loadFromDataBase:@"SELECT * FROM SEMESTER"];
    if (collection_rows == 0) {
        collection_flag = true;
    }

    collection_rows = (int)[semesters count] + 1;
    NSLog(@"Rows : %d",collection_rows);

    tabel.layer.borderColor = [UIColor blackColor].CGColor;
    tabel.layer.borderWidth = 2;
    tabel.layer.cornerRadius = 15;
    tabel.alpha = 0;

    [UIView animateWithDuration:2 animations:^{
        collection.alpha = 1;
    }];

    isTableVisible = NO;
}


-(void)viewWillAppear:(BOOL)animated
{
    [self viewDidLoad];


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"did select cell"]) {
        item = [[NSUserDefaults standardUserDefaults] integerForKey:@"selected cell"];
        prevCell = [NSIndexPath indexPathForItem:item inSection:0];
        NSLog(@"Cell item  = %d",item);
        }

    [collection reloadData];
}

1 个答案:

答案 0 :(得分:0)

如果您唯一的目的是更改所选的单元格背景并将其更改回以前的状态,如果选择了另一个单元格,那么您做错了。您应该使用委托方法来查找选定/取消选择的单元格。

// 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];

//changing the selected cell background on appreaing of uicollectionview
if (indexPath.item == prevCell.item) {
    cell.backgroundColor = [UIColor redColor];
}