Swift 中,#pragma mark 并不再支持判耕,要?jiǎng)澐址椒悇e的方法:
帶劃線分隔
//MARK: – Lifecycle
不帶劃線分隔
//MARK: UITableViewDataSource
另外 protocol 的使用標(biāo)識(shí)也不再是 <> 吆寨,現(xiàn)在的方法是:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
…
}
似乎是卡頓還是什么原因启妹,需要 build 后錯(cuò)誤提示才會(huì)消失靶擦。
在 storyboard 中布局一個(gè) UITableView 和一個(gè) UILabel,細(xì)化一個(gè) UITableViewCell 為 MyTableViewCell挺勿,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的瀑布流 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)項(xiàng)”
}
//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”)
}
}