简体   繁体   中英

collectionviewcell didSelectItemAtIndexPath/didDeselectItemAtIndexPath not responding for specific cells

I am trying to preselect some of the cell according to condition, which sets those cells selected and also change its background color while drawing those cells. Now the method

didSelectItemAtIndexPath / didDeselectItemAtIndexPath

is not getting called only for those preselected cells and hence I am not able to toggle selection and background color . The select/deselect delegate methods are being called for other cells

    -(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"cellForItemAtIndexPath: %@", indexPath);
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];

        if(indexPath.row != 0 && indexPath.row != 8 && indexPath.section != 0 && indexPath.section != 25){
            NSMutableDictionary *blockedHours = [blockedDaysArray objectAtIndex:indexPath.row-1];
            NSString *blockedVal = [blockedHours valueForKey:@(indexPath.section-1).stringValue];
            [cell setBlockedVal:(NSString*)blockedVal];
        }
        [cell addDayTimeLable:indexPath];
        return cell;
    }

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"didSelectItemAtIndexPath: %@ ", indexPath);
        NSMutableDictionary *blockedHours = [blockedDaysArray objectAtIndex:indexPath.row-1];
        [blockedHours setValue:@"1" forKey:@(indexPath.section-1).stringValue];
        CollectionViewCell *cell = (CollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
        cell.selected = YES;
    }

    -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"didDeselectItemAtIndexPath %@", indexPath);
        NSMutableDictionary *blockedHours = [blockedDaysArray objectAtIndex:indexPath.row-1];
        [blockedHours setValue:@"0" forKey:@(indexPath.section-1).stringValue];
        CollectionViewCell *cell = (CollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
        cell.selected = NO;
    }

In CollectionViewCell.m:

Self.selected calls the setter method and hence chagnes the background color

    -(void)setBlockedVal:(NSString*)blockedVal{
        if([blockedVal isEqualToString:@"1"]){
            self.selected = YES;
        }
    }


    -(void)setSelected:(BOOL)selected{
        NSLog(@"set selected: %d", selected);
        [super setSelected:selected];
        if(selected)
            self.backgroundColor = [SFIColors lightGreenColor];
        else
            self.backgroundColor = [UIColor whiteColor];
    }

Note:

(1) didHighlightItemAtIndexPath/didUnHighlightItemAtIndexPath are getting called for preselected cells.

(2)I Just found out that setting selected via didselect/didunselect is redundant and I just removed from my code.Noticed that setSeleted is auto called on clicking the other cells. Still this setSelected is not being for preselected cells

Any Inputs to fix this or another way that I can do my task would be of great help.

I found the answer in this link: UICollectionView - didDeselectItemAtIndexPath not called if cell is selected

I actually searched a lot actually, but only now I found this link.

I had let my collection view know about my selection and that did the trick:

[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM