这可能是一个非常简单的解决办法;我一直在阅读无数的论坛和教程,似乎无法找到答案。
在我的应用程序中,我有一个弹出窗口集合视图,用于选择特定选项并返回值...
但它不会突出显示或选择?我可以看到NS日志输出从不显示选择或数据空间。
这是我的代码......
collection.h文件
@interface collection : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSArray *counterImages;
@property (nonatomic, retain) NSArray *descriptions;
@property (nonatomic, weak) UILabel *graniteselect;
@property (nonatomic, retain) UIPopoverController* _collectionPopOver;
@property (nonatomic) BOOL clearsSelectionOnViewWillAppear; // defaults to YES, and if YES, any selection is cleared in viewWillAppear:
@property (nonatomic) BOOL allowsSelection;
@end
implementation file:
#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return _descriptions.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"MyCell"
forIndexPath:indexPath];
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:_counterImages[row]];
myCell.imageView.image = image;
myCell.celltext.text= [_descriptions objectAtIndex:row];
return myCell;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
_graniteselect.text = [_descriptions objectAtIndex:row];
[self._collectionPopOver dismissPopoverAnimated:YES];
NSLog(@"Setting Value for Granite Select");
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath: (NSIndexPath *)indexPath {
// TODO: Deselect item
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
RoleDetailTVC *destination =
[segue destinationViewController];
destination.graniteselect = _graniteselect;
NSLog(@"Passing Data to RoleDetailTVC");
}
@end
答案 0 :(得分:3)
这里有很多问题。首先,我认为你混淆了委托方法进行选择。 collectionView:performAction:forItemAtIndexPath:withSender:
方法仅适用于特殊菜单弹出窗口,与选择单元格本身无关。您需要在代理中实现的是:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected cell at index path %@", indexPath);
}
现在您只是实施取消选择回调。
除此之外,还要确保UICollectionViewCell
中的图片视图已启用触摸,否则您可能会阻止触摸事件到达单元格,并且它不会触发任何委托调用。我希望有所帮助!