这里使用的StoryBoard连线的delegate和dataSource
值得一提的是图片的赋值方式:
不仅仅可以使用cell.imageView?.image = UIImage(named: "xining")
;
还可以使用cell.imageView?.image = 图片
的形式[只要你将图片已经加入了Assets]
隐藏状态栏
override var prefersStatusBarHidden: Bool{
return true
}
注意再提一点
override func viewDidLoad() {
super.viewDidLoad()
}
这里的override
是子类覆盖父类方法的关键字
源码
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var areas = ["川1","川2","川3","重1","重2","重3","贵1","贵2","贵3"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return areas.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = areas[indexPath.row]
cell.imageView?.image = UIImage(named: "xining")
return cell
}
override var prefersStatusBarHidden: Bool{
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
补充:
静态tableView无需数据源相关代码,删除控制器中的 numberOfSections 和numberOfRowsInSection
网友评论