zoukankan      html  css  js  c++  java
  • Swift UICollectionView 简单使用

    最近要研究下排布的游戏关卡界面的实现,简单做了个UICollectionView的demo。

    先看最后的效果:

     

    下面来看实现的方法把,在Storyboard对应的ViewController中增加一个UICollectionView控件,然后再其中加入一个CollectionViewCell


    在其中增加一个Label控件

     

    注意,下面对这个Cell进行命名,命名成defaultCell, 这样我们UI层面的工作就结束了。

     

    代码部分:

    首先我们需要了解两个类,UICollectionViewDataSource和UICollectionViewDelegate
    UICollectionViewDataSource负责提供提供View所需要的数据源
    UICollectionViewDelegate负责处理View对应的各种事件

    class MyCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{
        
        @IBOutlet weak var cv: UICollectionView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            cv.dataSource = self
            cv.delegate = self
        }
        
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
    	//实现UICollectionViewDataSource
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
        {
    	//返回记录数
            return 100;
        }
        
      //实现UICollectionViewDataSource
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
        {
    	//返回Cell内容,这里我们使用刚刚建立的defaultCell作为显示内容
            var cell:MyColletionCell  = cv.dequeueReusableCellWithReuseIdentifier("defaultCell", forIndexPath: indexPath) as! MyColletionCell
            cell.label.text = "(indexPath.section):(indexPath.row)"
            return cell;
        }
        
        //实现UICollectionViewDataSource
        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
        {
            //某个Cell被选择的事件处理
        }
    }
    

     之后运行,你就可以看到效果啦。

  • 相关阅读:
    为什么多线程读写 shared_ptr 要加锁?
    iOS开发那些事iOS6 UI状态保持和恢复
    Hook钩子程序(续)
    JavaScript 项目构建工具 Grunt 实践:任务和指令
    浏览器插件之ActiveX开发(五)
    mongodb副本集架构搭建
    Hadoop 0.23.3版本
    Grails 中使用 grailseventspush 实现 ajax/comet/websocket 消息推送
    razor中@相对于<% %>的优势
    Chrome developer tool介绍(javascript调试)
  • 原文地址:https://www.cnblogs.com/chenlhuaf/p/4579303.html
Copyright © 2011-2022 走看看