首先创建model,新建一个文件存放model类
import UIKit
class testModel: NSObject {
var Title:String
init(title:String){
self.Title = title
}
}
在controller中的class外,声明一个变量
var list:[testModel] = []
为list增加数据
override func viewDidLoad() {
super.viewDidLoad()
list = [testModel(title:"蜻蜓"),
testModel(title:"蝴蝶"),
testModel(title:"燕子"),
testModel(title:"麻雀"),
testModel(title:"飞机"),
testModel(title:"大炮")]
}
将table view右键拖入类中绑定命名为listTable
@IBOutlet weak var listTable: UITableView!
为table view cell 命名 todoCell
在cell中放置一个label,并设置tag属性
让controller类继承UITableViewDataSource
协议,并实现协议:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return list.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.listTable.dequeueReusableCellWithIdentifier("todoCell")! as UITableViewCell
let todo = list[indexPath.row] as testModel
let label = cell.viewWithTag(110) as! UILabel
label.text = todo.Title
return cell
}
右击storyboard中右击table view拖拉至控制器,选择dataSource,运行后,可见效果
本是自己的学习笔记,可能写的比较粗俗,欢迎吐槽