当我输入var类型变量时Xcode显示警告
变量永远不会改变
如果我使用let类型变量,那么不显示任何结果!
import UIKit
class ViewController: UIViewController,UITableViewDataSource{
let people = [
("Pankaj","Dhaka"),
("Asish","Madaripur"),
("Anup","Narail")
]
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "People"
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let (personName , personLocation) = people[indexPath.row]
cell.textLabel?.text = personName
cell.textLabel?.text = personLocation
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
不能使用默认初始化器UITableViewCell()
初始化的表视图单元格
重用单元格,在Interface Builder中添加标识符(例如PeopleCell
)。
let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleCell", for: indexPath)
并确保表视图的datasource
和delegate
也在Interface Builder中连接。