基本表格视图问题

时间:2018-10-08 07:06:33

标签: swift debugging tableview

我对Swift还是很陌生,我试图建立一个超级简单的表视图,但我尝试过的任何方法都没有用。它的构建没有问题,但是我的表在模拟器上不可见。我在做什么错了?

class ViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {

var currentNames: Array = ["Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10"]
var currentDays: Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
@IBOutlet weak var tableView: UITableView!

public func tableView(_ tableView: UITableView, 
numberOfRowsInSection section: Int) -> Int {
   return currentNames.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let currDays = currentDays[indexPath.row]
    cell.textLabel?.text = currentNames[indexPath.row]
    cell.detailTextLabel?.text = String(currDays)
    return(cell)
}

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

1 个答案:

答案 0 :(得分:0)

在创建tableView出口后,请确认您已在ViewDidLoad中使用以下行

tableView.delegate = self
tableView.dataSource = self

class ViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {

 @IBOutlet weak var tableView: UITableView!

 override func viewDidLoad() {
     super.viewDidLoad()
     tableView.delegate = self
     tableView.dataSource = self
 }

var currentNames: Array = ["Name1", "Name2", "Name3", "Name4", "Name5", 
    "Name6", "Name7", "Name8", "Name9", "Name10"]
var currentDays: Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return currentWorkoutNames.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let currDays = currentDays[indexPath.row]
    cell.textLabel?.text = currentNames[indexPath.row]
    cell.detailTextLabel?.text = String(currDays)
    return(cell)
}