美文网首页无法删除的专题
Swift Pragma Mark and Protocol

Swift Pragma Mark and Protocol

作者: Jiubao | 来源:发表于2016-03-28 18:10 被阅读41次

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”)

}

}

相关文章

网友评论

    本文标题:Swift Pragma Mark and Protocol

    本文链接:https://www.haomeiwen.com/subject/xtgjlttx.html