繁体   English   中英

尚未调用TableView的cellForRowAtIndexPath吗?

[英]cellForRowAtIndexPath for TableView not being called?

我正在尝试在Swift中构建一个简单的TableView,但是我不想创建ViewController中的数据源协议,而是想创建一个新类来用作DataSource。 不幸的是,使用这种方法,我无法在ViewController中加载任何内容。

这是我的ViewController类:

class SaladViewController: UIViewController {

    @IBOutlet weak var saladTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let tableData = LunchTableData()
        self.saladTable.dataSource = tableData
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

这是我的DataSource类:

class LunchTableData: NSObject, UITableViewDataSource {

    var things = ["One", "Two", "Three"]

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")

        let itemText = cell!.viewWithTag(100) as! UILabel
        itemText.text = things[indexPath.row]

        return cell!
    }
}

RowAtIndexPath被调用的很好,所以我只是不确定为什么从未调用CellForRowAtIndexPath。 我已经设置了一个断点,但它从未实现。

感谢您的帮助!

我认为tableData在viewDidLoad()完成之后被破坏了。

因此,将以下行移到@IBOutlet弱var SaladTable下方:UITableView! 线。

let tableData = LunchTableData()

希望这对您有帮助

如此完整的代码

class SaladViewController: UIViewController {

    @IBOutlet weak var saladTable: UITableView!

    let tableData = LunchTableData()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.saladTable.dataSource = tableData
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM