0

这可能是一个非常容易解决的问题;我一直在阅读无数的论坛和教程,似乎无法找到答案。

在我的应用程序中,我有一个弹出集合视图来选择特定选项并返回一个值...

但是它不会突出显示或选择?我可以看到 NS 日志输出从不显示选择或数据传递。

这是我的代码...

集合.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
4

1 回答 1

3

这里有很多问题。首先,我认为您混淆了选择的委托方法。该方法仅适用于特殊菜单弹出框,与选择单元格本身collectionView:performAction:forItemAtIndexPath:withSender:无关。你需要在你的委托中实现的是:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected cell at index path %@", indexPath);
}

现在你只是在实现取消选择回调。

除此之外,还要确保您的图像视图中的UICollectionViewCell触摸已启用,否则您可能会阻止触摸事件到达单元格并且它不会触发任何委托调用。我希望这会有所帮助!

于 2013-05-20T12:10:53.107 回答