Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CollectionView not Responding to Tapped Index List Item #35

Open
MaikoHermans opened this issue Jan 23, 2017 · 4 comments
Open

CollectionView not Responding to Tapped Index List Item #35

MaikoHermans opened this issue Jan 23, 2017 · 4 comments

Comments

@MaikoHermans
Copy link

MaikoHermans commented Jan 23, 2017

I'm trying to use your framework to be able to get an index list for my collection view.
Before actually doing this inside of my project I figured it would be useful to make a test project to test the working and functionality of it first.

this is my entire controller. I'm using swift 3
do you have any idea why it might not be working? is there something I'm doing wrong?

Basically indexViewValueChanged is never called

import UIKit
import BDKCollectionIndexView

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var indexView: BDKCollectionIndexView!
    
    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]

    override func viewDidLoad() {
        super.viewDidLoad()
        let indexWidth: CGFloat = 28

        let frame = CGRect(x: (collectionView.frame.size.width - indexWidth),
                           y: collectionView.frame.size.height,
                           width: indexWidth,
                           height: collectionView.frame.size.height)

        let indexView = BDKCollectionIndexView.init(frame: frame, indexTitles: nil)
        indexView?.autoresizingMask = [.flexibleHeight, .flexibleLeftMargin]
        indexView?.addTarget(self, action: Selector("indexViewValueChanged:"), for: .valueChanged)
        view.addSubview(indexView!)
        self.indexView.indexTitles = items
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }
    
    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
        
        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.myLabel.text = self.items[indexPath.item]
        cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
        
        return cell
    }
    
    // MARK: - UICollectionViewDelegate protocol
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }

    func indexViewValueChanged() {
        let path = "hello"
        print(path)
    }
    
    func indexViewValueChanged(sender: BDKCollectionIndexView) {
        let path = IndexPath(item: 0, section: Int(sender.currentIndex))
        collectionView.scrollToItem(at: path, at: .top, animated: false)
        // If you're using a collection view, bump the y-offset by a certain number of points
        // because it won't otherwise account for any section headers you may have.
        collectionView.contentOffset = CGPoint(x: collectionView.contentOffset.x,
                                               y: collectionView.contentOffset.y - 45.0)
    }
}
@athulsai
Copy link

@MaikoHermans try this indexView!.addTarget(self, action: #selector(ViewController.indexViewValueChanged(sender:)), for: .valueChanged)

@tamoyal
Copy link

tamoyal commented Oct 26, 2017

+1 ...it just doesn't work

@kreeger
Copy link
Owner

kreeger commented Oct 27, 2017

Yeah, like @athulsai said — I think you'll need to update your syntax to the new Swift 3 #selector format, and it's likely (?) you'll need to mark your action method to be @objc.

override func viewDidLoad() {
    // ...
    indexView?.addTarget(self,
                         action: #selector(indexViewValueChanged(sender:)),
                         for: .valueChanged)
}

// ...

@objc func indexViewValueChanged(sender: BDKCollectionIndexView) {
    // ...
}

@rjalkuino
Copy link

I already using it in my apps , also in Swift 4 alreay but I dont know my action didn't called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants