[英]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
. 不会仅针对那些
preselected
单元格被调用,因此我无法切换选择和background color
。 The select/deselect delegate methods
are being called for other cells select/deselect delegate methods
正在为其他单元格调用
-(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: 在CollectionViewCell.m中:
Self.selected calls the setter method and hence chagnes the background color Self.selected调用setter方法,因此改变了背景色
-(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.
(1)didHighlightItemAtIndexPath / didUnHighlightItemAtIndexPath正在为预选单元格调用。
(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.
(2)我刚刚发现通过didselect / didunselect选择的设置是多余的,我刚刚从代码中删除了。注意到setSeleted是在单击其他单元格时自动调用的。 Still this setSelected is not being for preselected cells
这个setSelected仍然不是针对预选单元的
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 我在此链接中找到了答案: UICollectionView-如果选择了单元格,则不会调用didDeselectItemAtIndexPath
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.