未调用cellForRowAtIndex路径

时间:2015-04-19 18:54:08

标签: ios swift

即使在第一个视图或表格输入之后,也不会调用函数 cellForRowAtIndexPath 。这是待办事项列表应用程序的代码片段。

import UIKit

class FirstViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {


    @IBOutlet var tblTasks: UITableView!

    let defaults = NSUserDefaults.standardUserDefaults()


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return taskMgr.tasks.count
    }

    **//THIS FUNCTION IS NOT BEING CALLED.
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //Check if Function is being called or not
        println("hello")
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")
        cell.textLabel!.text=taskMgr.tasks[indexPath.row].name
        cell.detailTextLabel!.text=taskMgr.tasks[indexPath.row].name

        return cell
    }**

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if(editingStyle==UITableViewCellEditingStyle.Delete)
        {
            taskMgr.tasks.removeAtIndex(indexPath.row)
            tblTasks.reloadData()
        }

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        println(taskMgr.tasks[indexPath.row])
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        tblTasks.delegate = self
        tblTasks.dataSource = self
        tblTasks.reloadData()
        // 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.
    }


}

1 个答案:

答案 0 :(得分:0)

解决使用冲浪时发现的这种方法:

你可以这样做:

在tableView Controller中:

override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
    }

   func loadList(notification: NSNotification){
        //load data here
        self.tblTasks.reloadData()
    }

然后在另一个ViewController中:

NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)