collectionView的didSelectItemAtIndexPath无法正常工作[Swift]

时间:2016-06-09 08:17:32

标签: swift uicollectionview

我最近遇到过这个问题,而且我似乎找不到任何其他类似问题的解决办法。我有一个只显示颜色的collectionView(见下图)我希望将单元格的背景颜色存储在变量中以通过展开segue,以便设置另一个元素的背景颜色。但是,我似乎无法使此功能像以前的项目和此应用程序的版本一样工作,我想知道是否有人知道问题可能是什么?

CollectionView

按下底部的“添加”按钮后,它会展开并将新项目附加到另一个collectionView,如下所示:

Other

我能够获取用户输入的名称,但是我必须使用默认颜色,因为我似乎无法使didSelectItemAtIndexPath功能起作用。

这是我的addViewController代码(第一张图片)

import UIKit
import ChameleonFramework

class mondayViewController: UIViewController {

    // collectionView
    @IBOutlet var dayView: UICollectionView!

    // data to display
    var data = Day.mondayData()

    // data from segues
    var receivedName: String?
    var receivedColour: UIColor?

    override func viewDidLoad() {
        super.viewDidLoad()

        // set title
        self.navigationController!.navigationBar.topItem!.title = "Monday"
        // set datasource for collectionView 
        // datasource is self as the collectionViewDataSource methods are in the extension class below
        dayView.dataSource = self

        // make the navigationBar flat to flow onto the options tab
        self.navigationController?.hidesNavigationBarHairline = true


    }

    @IBAction func unwindToMonday(segue: UIStoryboardSegue) {


        if segue.identifier == "mondayUnwind" {

            let subjectAdd = segue.sourceViewController as! mondayAddSubjectView

            receivedName = subjectAdd.nameField.text

            appendStuff()
        }




    }

    func appendStuff() {



        data.append(Day(name: receivedName, color: UIColor.flatRedColor()))
        dayView.reloadData()


    }

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }



//    data.append(Day(name: receivedName, color: receivedColour))
//    dayView.reloadData()


    // currently used for testing
    @IBAction func mondayAddButton(sender: AnyObject) {}


}


// mandatory functions for UICollectionView to work
// this includes assigning the number of sections, how manycells are meant to be in the section(s) and what cell to use (mondayCell in this case)

extension mondayViewController: UICollectionViewDataSource {

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return data.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! mondayCell
        cell.data = self.data[indexPath.item]


        return cell
    }



    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath)

    }



    // custom style the size of the collectionView. Changes the width to fill the screen and the height to 70

    func collectionView(collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                               sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let kWhateverHeightYouWant = 80
        return CGSizeMake(collectionView.bounds.size.width, CGFloat(kWhateverHeightYouWant))
    }




}

如果有人能让我知道我做错了什么,或者解决了这个问题,那就太棒了。

干杯,

罗文

更新:

我已将collectionViewDataSource和Delegate方法添加到主类(下面的代码):

import UIKit
import ChameleonFramework


class mondayAddSubjectView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var pageView: PageViewController = PageViewController()



    @IBOutlet var selectColourLabel: UILabel!
    // would be a public variable, however it is an internal class
    internal var cellColour: UIColor!

    // collectionView
    @IBOutlet var colourPicker: UICollectionView!


    // textfield for the user to input their subject name
    @IBOutlet var nameField: UITextField!

    // source of colours to use in the colour collectionView
    private var data = collectionViewColors.createColor()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.colourPicker.delegate = self


        colourPicker.dataSource = self
        // gesture recognizer to determine when the user has tapped anywhere but the text field, which cancells the keyboard

        let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(mondayAddSubjectView.dismissKeyboard))
        view.addGestureRecognizer(tap)


    }

    // dismiss keyboard when the user taps away from the text field (called above with #selector)
    func dismissKeyboard() {

        view.endEditing(true)
    }
    // MARK: - CollectionView Delegate and Data source methods


    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath)



        print("it works")

    }

    // choose how many sections are in the collectionView. as no content break is required, this will be 1
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    // choose how many items are in each section. as there is only 1 section, it will be the number of stored items
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return data.count
    }
    // set re-use identifier and the data that will go in each cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! colourCell
        cell.data = self.data[indexPath.item]
        return cell
    }







}

0 个答案:

没有答案