UITableView单元格未在Swift中更新

mat*_*bor 1 uitableview ios swift

我正在尝试在UIViewController内的UITableView中设置单元格的文本,并且文本没有被更新.此外,即使我有两个打印语句,也没有打印任何内容,因此似乎没有调用这些函数.这是我的代码:

class HomePageViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate {
@IBOutlet weak var flaggedTable: UITableView!
@IBOutlet weak var recentTable: UITableView!

var flaggedSerials = [DataCell]()
var recentlyViewedSerials = [DataCell]()

override func viewDidLoad() {
    super.viewDidLoad()

    addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)    
}    

func addSerial(serial: String, status: String, issues: Int, reworks: Int, index: Int, flagged: Bool)
{
    var currentSerial = DataCell()

    currentSerial.serial = serial
    currentSerial.status = status
    currentSerial.issues = "\(issues) Reported Issues"
    currentSerial.reworks = "\(reworks) Reported Reworks"
    currentSerial.index = index

    if flagged == true {
        flaggedSerials.append(currentSerial)
    }
    else {
        recentlyViewedSerials.append(currentSerial)
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        println(recentlyViewedSerials.count) // This line does not get printed
        return recentlyViewedSerials.count
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("FlaggedCell", forIndexPath: indexPath) as! UITableViewCell
    addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)
    println(recentlyViewedSerials[indexPath.row].serial) // This line does not get printed
    cell.textLabel?.text = recentlyViewedSerials[indexPath.row].serial

    return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

asi*_*ohd 5

用这一行替换类定义行:

class HomePageViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

还记得将tableView 的delegatedatasource属性设置为self.

您可以使用界面构建器或使用viewDidLoad方法中的代码来执行此操作:

override func viewDidLoad() {
super.viewDidLoad()

// do this for all your tableViews and make sure to have an outlet configured first
tableView.delegate = self
tableView.datasource = self
addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)    
}   
Run Code Online (Sandbox Code Playgroud)