![](/img/trans.png)
[英]Need to shake a collectionviewcell on didselectitematindexpath in ios
[英]collectionviewcell didSelectItemAtIndexPath/didDeselectItemAtIndexPath not responding for specific cells
我正在尝试根据条件预先选择一些单元格,这将设置那些选定的单元格,并在绘制这些单元格时更改其背景颜色。 现在的方法
didSelectItemAtIndexPath / didDeselectItemAtIndexPath
不会仅针对那些preselected
单元格被调用,因此我无法切换选择和background color
。 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;
}
在CollectionViewCell.m中:
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];
}
注意:
(1)didHighlightItemAtIndexPath / didUnHighlightItemAtIndexPath正在为预选单元格调用。
(2)我刚刚发现通过didselect / didunselect选择的设置是多余的,我刚刚从代码中删除了。注意到setSeleted是在单击其他单元格时自动调用的。 这个setSelected仍然不是针对预选单元的
任何解决此问题或其他可以完成我的任务的方法的输入都会有很大的帮助。
我在此链接中找到了答案: UICollectionView-如果选择了单元格,则不会调用didDeselectItemAtIndexPath
实际上,我实际上进行了很多搜索,但是直到现在我才找到此链接。
我已经让我的收藏夹视图知道了我的选择,然后做到了:
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.