(二)swift 学习之创建 tableview
2016-05-18 本文已影响71人
马爷
这个是 swift 中添加 tableview 的代理
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
接下来就是创建 tableview
func creatTableview() {
let tableview = UITableView()
self.view.addSubview(tableview)
tableview .snp_makeConstraints { (make) in
make.edges.equalTo(self.view).inset(UIEdgeInsetsMake(10, 10, 10, 10));
}
tableview.backgroundColor = UIColor.brownColor()
tableview.delegate = self;
tableview.dataSource = self;
tableview.registerClass(UITableViewCell.self, forCellReuseIdentifier: "swiftCell")
}
接下来写 tableview 的代理方法
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let indent : String = "swiftCell"
let cell = tableView.dequeueReusableCellWithIdentifier(indent, forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "swift"
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44
}