[英]I have a custom UICollectionViewCell that was created using an XIB file.How do I get this cell to push another controller onto the stack when tapped
I can't drag and create a segue in interface builder as this is an XIB file. 我不能在界面生成器中拖动和创建序列,因为这是XIB文件。 Basically I have a UICollectionView that has two layouts.
基本上我有一个UICollectionView具有两个布局。 The standard layout uses a uses one custom cell and the other layout users another custom cell.
标准布局使用一个使用自定义单元格,而另一布局使用另一个自定义单元格。
The first custom cell was easy to connect up as it wasn't an XIB file so I just dragged and connected it up to the detail view I wanted to push to when it was tapped. 第一个自定义单元很容易连接,因为它不是XIB文件,所以我将其拖动并连接到要在点击时推入的详细视图。 However the second custom cell is the XIB one and there can't be connected up this way.
但是,第二个自定义单元是XIB单元,无法以这种方式连接。
This is my collection view cellForRowAtIndexPath method: 这是我的集合视图cellForRowAtIndexPath方法:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
// NSLog(@"cellForItemAtIndexPath");
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";
VAGGarmentCell *cell;
if ([[_collectionView collectionViewLayout] isEqual: _flowLayout]) {
cell = [_collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
} else if ([[_collectionView collectionViewLayout] isEqual: _flowLayout2]) {
cell = [_collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier2 forIndexPath:indexPath];
// // Detect tap of image View
UITapGestureRecognizer *gestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapGarmentCellTwoImage:)];
[gestureRecogniser setNumberOfTapsRequired:1];
[[cell imageView] addGestureRecognizer:gestureRecogniser];
[[cell imageView] setUserInteractionEnabled: YES];
}
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
// if database favourites status is equal to one then set highlighted to (yes)
UIButton *addFavouriteButton = [cell addFavouriteButton];
BOOL objectFavourited = [object[@"favourite"] boolValue]; // I set this when add fav button is tapped
[addFavouriteButton setSelected: objectFavourited];
[addFavouriteButton addTarget:self action:@selector(addFavouriteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)didTapGarmentCellTwoImage:(UITapGestureRecognizer *)sender
{
[self performSegueWithIdentifier:@"garmentDetailSegue" sender:self];
}
This way is giving me an error: 这种方式给我一个错误:
[UITapGestureRecognizer _layoutAttributes]: unrecognized selector sent to instance 0xc7f3290'
This just seems messy. 这看起来很混乱。 Is there a better way to do this?
有一个更好的方法吗? Dragging and connecting with the other cell was so much easier.
拖动和与其他单元格连接非常容易。
Have you tried to use collectionView:didSelectItemAtIndexPath: without adding TapGesture on your cell. 您是否尝试过使用collectionView:didSelectItemAtIndexPath:而不在单元格上添加TapGesture。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if ([[_collectionView collectionViewLayout] isEqual: _flowLayout]) {
//do what you want with the other cell selection
}
else if ([[_collectionView collectionViewLayout] isEqual: _flowLayout2]) {
[self performSegueWithIdentifier:@"garmentDetailSegue" sender:self];
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.