zoukankan      html  css  js  c++  java
  • 【Swift Mac开发】通过纯代码的方式创建NSCollectionView (Mac OS X)

      NSCollectionView

    let layout = NSCollectionViewFlowLayout()
    layout.minimumLineSpacing = 4
    
    collectionView = NSCollectionView()
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.collectionViewLayout = layout
    collectionView.allowsMultipleSelection = false
    collectionView.backgroundColors = [.clear]
    collectionView.isSelectable = true
    collectionView.register(
      Cell.self,
      forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell")
    )

      NSScrollView

    scrollView = NSScrollView()
    scrollView.documentView = collectionView
    view.addSubview(scrollView)

      NSCollectionViewItem

    final class Cell: NSCollectionViewItem {
      let label = Label()
      let myImageView = NSImageView()
    
      override func loadView() {
        self.view = NSView()
        self.view.wantsLayer = true
      }
    }

      NSCollectionViewDataSource

    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
      return coins.count
    }
    
    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
      let cell = collectionView.makeItem(
        withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"),
        for: indexPath
      ) as! Cell
    
      let coin = coins[indexPath.item]
    
      cell.label.stringValue = coin.name
      cell.coinImageView.image =
        NSImage(named: NSImage.Name(rawValue: "USD"))
        ?? NSImage(named: NSImage.Name(rawValue: "Others"))
    
      return cell
    }

      NSCollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
        guard let indexPath = indexPaths.first,
        let cell = collectionView.item(at: indexPath) as? Cell else {
          return
      }
    }
    
    func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
      guard let indexPath = indexPaths.first,
        let cell = collectionView.item(at: indexPath) as? Cell else {
          return
      }
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
    
      return NSSize(
         collectionView.frame.size.width,
        height: 40
      )
    }

    分享链接:

    工作之余,开了一个淘宝小店,分别销售日常必备生活用品,期待您的光临!点击下图,跳转店铺首页!
  • 相关阅读:
    杜马岛
    Type interface com.zhaoka.mapper.DatKcardKmMapper is not known to the MapperRegistry
    jsp实现自动登录
    Struts2中的get、set方法重要性 .
    struts2 通过前台标签name属性将值传到后台,没有name属性传值,则后台对象有默认值,不为null。
    Jsp 操作 Cookie 实现自动登录
    struts2的bean类名首字母和第二个字母都不能大写
    mybatis自动生成的ExamMapper.xml方法总结
    Mybatis插入时注意,没有主键值。所以主键一般为自增类型
    <ywaf:code code="${project.projectType}" typeCode="PROJECT_TYPE"/>
  • 原文地址:https://www.cnblogs.com/xjf125/p/14751109.html
Copyright © 2011-2022 走看看