//原始写法
import UIKit
class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{
override func loadView() {
let tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.frame = UIScreen.mainScreen().bounds
view = tableView
}
lazy var dataList = {["小史","教授","小飞","陈伟"]}()
//MARK: - UITableView的数据源方法和代理方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = dataList[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataList.count
}
}
//苹果官方建议将代理和数据源方法单独写道一个扩展中,以便于提高代码可读性
import UIKit
class ViewController: UIViewController {
override func loadView() {
let tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.frame = UIScreen.mainScreen().bounds
view = tableView
}
lazy var dataList = {["小史","教授","小飞","陈伟"]}()
}
//MARK: - UITableView的数据源方法和代理方法
extension ViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = dataList[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataList.count
}
}
swift UITableView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 最近打算开始学习Swift,记录一下自己的学习路程吧 ~UITableView基本算是最基础的,同时也是我们经常使...
- var tableview = UITableView() // 创建可变数组 var datas: NSMut...