无法在集合视图中选择项目

时间:2016-09-19 20:34:18

标签: ios swift uicollectionview

我尝试将此代码用于collectionView How to make a simple collection view with Swift

但对于swift 3我在viewController中添加了这些更改

    import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    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"]


    // MARK: - UICollectionViewDataSource protocol

    // tell the collection view how many cells to make
    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! 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.yellow // make cell more visible in our example project

        return cell
    }

    // MARK: - UICollectionViewDelegate protocol

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }
}

它可以工作,但在调试区域我得到了这个

    2016-09-20 00:18:39.299329 retry[6595:491717] subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 00:18:39.319250 retry[6595:491717] subsystem: com.apple.UIKit, category: HIDEventIncoming, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 00:18:39.375574 retry[6595:491699] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0
2016-09-20 00:18:39.436027 retry[6595:491572] subsystem: com.apple.UIKit, category: StatusBar, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 00:18:39.689342 retry[6595:491572] subsystem: com.apple.BackBoardServices.fence, category: App, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0
2016-09-20 00:18:56.028502 retry[6595:491572] subsystem: com.apple.UIKit, category: Touch, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 00:18:56.031374 retry[6595:491572] subsystem: com.apple.UIKit, category: Gesture, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 00:18:56.034089 retry[6595:491572] subsystem: com.apple.UIKit, category: GestureEnvironment, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 00:18:56.035698 retry[6595:491572] subsystem: com.apple.UIKit, category: GestureExclusion, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 02:07:46.530726 retry[6595:491572] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/joodi/Library/Developer/CoreSimulator/Devices/1DF7F969-AD53-4D2B-82B5-4999B5FC23E8/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2016-09-20 02:07:46.531996 retry[6595:491572] [MC] Reading from private effective user settings.

当我按任何单元格时没有任何反应,我认为didSelectItemAtIndexPath无法正常工作

1 个答案:

答案 0 :(得分:2)

:一种。didSelectItemAtIndexPath

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}

如果您需要有关声明功能的更多信息,请参阅 Apple Swift 3 文档。

B.指定环境变量以禁用不需要的日志

  1. 转到产品 - >方案 - >在Xcode菜单上编辑方案

    enter image description here

  2. 左栏中,选择运行计划操作。

  3. 点击右侧列顶部的参数
  4. 要添加环境变量,请单击“环境变量”表底部的“添加”按钮(+),并将变量名称输入为OS_ACTIVITY_MODE,值为{{ 1}}或DISABLE。然后点击关闭。

    enter image description here

  5. 如果您感到困惑,请参阅 video

    如果您需要有关Xcode的更多信息,请参阅Apple Xcode Help 页面。