Swift Pragma Mark and Protocol

Swift 中,#pragma mark 并不再支持,要划分方法类别的方法:

带划线分隔

//MARK: – Lifecycle

不带划线分隔

//MARK: UITableViewDataSource

另外 protocol 的使用标识也不再是 <> ,现在的方法是:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

}

似乎是卡顿还是什么原因,需要 build 后错误提示才会消失。

在 storyboard 中布局一个 UITableView 和一个 UILabel,细化一个 UITableViewCell 为 MyTableViewCell,实现一个简单的瀑布流 UI,ViewController 部分代码如下,可用于参考基本的方法分类及使用

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var countLabel: UILabel!

@IBOutlet weak var tableView: UITableView!

var myDataAry = NSMutableArray()

//MARK: – Lifecycle

override func viewDidLoad() {

super.viewDidLoad()

loadData()

tableView.reloadData()

p_updateUI()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

//MARK: – Methods

func loadData() {

myDataAry = [“1″,”2″,”3”]

}

func p_updateUI() {

countLabel.text = “总共\(myDataAry.count)项”

}

//MARK: – Actions

//MARK: UITableViewDataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return myDataAry.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:MyTableViewCell = tableView.dequeueReusableCellWithIdentifier(“tableviewcell”) as! MyTableViewCell

cell.backgroundColor = UIColor.redColor()

cell.myLabel.text = myDataAry[indexPath.row] as? String

return cell

}

//MARK: UITableViewDelegate

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

print(“tapped”)

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容