简体   繁体   English

我有一个使用XIB文件创建的自定义UICollectionViewCell.tap时如何使该单元格将另一个控制器推入堆栈

[英]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.

相关问题 在以模态方式呈现视图控制器时,如何将视图控制器从搜索结果推入导航堆栈? - How do I push a view controller onto the nav stack from search results when presenting them modally? 如何从自定义集合视图单元格(使用xib创建的单元格)到tabBar控制器创建自定义序列 - How can I create a custom segue from a custom collection view cell (cell created using xib) to a tabBar controller 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 当在 Swift 5 中点击 Tableview Row 时,如何使用 Xib(不是 StoryBoards)在 SideMenu Controller 中设置 UINavigationController 以推送新的 ViewController - How to set UINavigationController in SideMenu Controller using Xib (Not StoryBoards) to push new ViewController when Tableview Row Tapped in Swift 5 使用Xib文件的自定义tableview单元格,无法单击/选择/点击 - Cell of custom tableview using xib file, can not be clicked / selected / tapped 如何从带有XIB文件的另一个UIView中获取值? - How do I get a value from another UIView with XIB file? 我在视图控制器中显示了一个UIView,如何检测用户何时在此视图外部轻敲? - I have a UIView presented in a view controller, how do I detect when the user tapped outside of this view? 如何更改已点击的UICollectionViewCell的背景图像? - How do I change the background image of a UICollectionViewCell that has been tapped? 如何使用填充单元格的UIImageView初始化自定义UICollectionViewCell? - How do I initialize a custom UICollectionViewCell with a UIImageView that fills the cell? 轻按时如何在UICollectionViewCell中制作此动画? - How I could make this animation in a UICollectionViewCell when tapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM