使用NSIndexPath

时间:2018-04-23 14:34:27

标签: ios uicollectionview uicollectionviewcell nsindexpath

我在我的应用程序中使用了一个集合视图,选择了单元格..

要处理选择,我使用NSIndexPath didSelectItemAtIndexPathdidDeselectItemAtIndexPath中的didDeselectItemAtIndexPath

选择似乎工作正常但是当我在附近选择两个单元格时,似乎@property (nonatomic, strong) NSIndexPath *selectedIndex; @implementation YearSelector -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { YearCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:yearCellID forIndexPath:indexPath]; cell.yearLabel.text = [NSString stringWithFormat:@"%@", self.yearArray[indexPath.item]]; if ([indexPath isEqual:self.selectedIndex]) [cell highlightedYear:YES]; else [cell highlightedYear:NO]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSIndexPath *newIndex = [NSIndexPath indexPathForItem:indexPath.item inSection:0]; [collectionView scrollToItemAtIndexPath:newIndex atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO]; YearCell *cell = (YearCell *)[collectionView cellForItemAtIndexPath:indexPath]; _selectedIndex = indexPath; [cell highlightedYear:YES]; } -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { YearCell *cell = (YearCell *)[collectionView cellForItemAtIndexPath:indexPath]; [cell highlightedYear:NO]; _selectedIndex = nil; } 没有被调用...我在哪里做错了?

这是我的代码

-(void)highlightedYear:(BOOL)highlighted {
    if (highlighted) {
        _baseView.backgroundColor = [UIColor colorWithHexString:@"#FF7751" setAlpha:1];
        _baseView.layer.borderColor = UIColor.clearColor.CGColor;
        _yearLabel.textColor = UIColor.groupTableViewBackgroundColor;
    }

    else {
        _baseView.backgroundColor = [UIColor colorWithHexString:@"#272D3D" setAlpha:1];
        _baseView.layer.borderColor = [UIColor colorWithHexString:@"#485674" setAlpha:1].CGColor;
        _yearLabel.textColor = UIColor.groupTableViewBackgroundColor;
    }
}

这是我用于突出显示的单元格(自定义年份单元格)的代码

HeaderComponent

enter image description here

0 个答案:

没有答案