![](/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.