【问题标题】:Action for header in UICollectionView IOSUICollectionView IOS 中的标头操作
【发布时间】:2014-07-22 09:47:01
【问题描述】:

我的代码集合中有不同数量的标题,当我单击标题时,每个标题都有不同的操作。如果我选择一个单元格,则会调用 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 方法。因此,在选择任何补充视图时,是否有任何委托方法?

【问题讨论】:

  • 您可以将UITapGestureRecognizer 添加到您的视图中
  • 感谢您的帮助,但是通过使用 UITapGestureRecognizer 我必须为不同的标头分配不同的功能,我想要一种可以通过不同的 indexpath 用于所有标头的方法,是否可能

标签: ios objective-c uicollectionview


【解决方案1】:

这是@Geet 对 Swift 5 的回答。使用委托只有 9 个简单的步骤

// 1. create a protocol outside of your headerView class
protocol YourHeaderViewDelegate: class {

    // 2. create a function that will do something when the header is tapped
    func doSomething()
}

class YourHeaderView: UICollectionReusableView {

    // 3. inside the headerView set a property named delegate of type YourHeaderViewDelegate? and make sure it's **weak**
    weak var delegate: YourHeaderViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)

        // 4. add a tapGesture to your headerView
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(headerViewTapped))
        addGestureRecognizer(tapGesture)
    }

    // 5. inside the selector method for the tapGestureRecognizer call the property and then call the doSomething method
    @objc func headerViewTapped() {

        delegate?.doSomething()
    }
}

// 6. make sure the class with the headerView's collectionView conforms to YourHeaderViewDelegate
class ClassWithCollectionView: YourHeaderViewDelegate {

    // 7. inside the class with your collectionView when you create the headerView make to set the delegate as self
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerViewId", for: indexPath) as! YourHeaderView
        
        // 8. *** set this to self ***
        headerView.delegate = self
        
        return headerView
    }

    // 9. since your class conforms to the YourHeaderViewDelegate method when you tap the headerView this function will get called
    func doSomething() {

        print("hello!")
    }
}

【讨论】:

    【解决方案2】:

    尝试在您的视图中添加访客,例如

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(handleTapGuesture)];
    
    [headerView addGestureRecognizer:tap];
    

    将此代码放置在您设置 HeaderView 的位置

    然后实现 -(void)handleTapGuesture;执行您的操作的方法

    【讨论】:

    • 感谢您的帮助,但是通过使用上述方法,我必须为不同的标头分配不同的功能,我想要一种可以用于不同索引路径的所有标头的方法,是否可能
    • 然后尝试在方法中传递Section No.,然后根据SectionNo保持chi。
    【解决方案3】:

    如果需要使用indexpath做某事,也可以使用闭包来实现。根据@Geet的回答

    class headerView: UICollectionReusableView {
        var headerDidSelected: (()->())?
    
        ...
    
        func handleTapGuesture() {
            self.headerDidSelected?()
        }
    }
    
    class viewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerViewId", for: indexPath) as! headerView
            
            headerView.headerDidSelected = { [weak self] in
                print(indexPath)
                ...
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多