Skip to content

CollectionView not Responding to Tapped Index List Item #35

@MaikoHermans

Description

@MaikoHermans

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)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions