Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我无法使UITableViewController显示任何数据_Ios_Swift_Uitableview_Parse Platform_Programmatically Created - Fatal编程技术网

Ios 我无法使UITableViewController显示任何数据

Ios 我无法使UITableViewController显示任何数据,ios,swift,uitableview,parse-platform,programmatically-created,Ios,Swift,Uitableview,Parse Platform,Programmatically Created,我已经干了差不多两天了。我不能让它显示任何东西。我要一步一步走,因为我不知道问题出在哪里 我从一个空的Main.storyboard开始 我将表视图控制器拖动到Main.storyboard 我将表视图控制器嵌入到导航控制器中 我创建了一个Cocoa Touch类UITableViewController文件。我把它命名为TableViewController 我在拖动的表视图控制器的标识检查器窗格中的自定义类下的类文本字段中键入TableViewController 我创建了一个Cocoa T

我已经干了差不多两天了。我不能让它显示任何东西。我要一步一步走,因为我不知道问题出在哪里

我从一个空的Main.storyboard开始

我将表视图控制器拖动到Main.storyboard

我将表视图控制器嵌入到导航控制器中

我创建了一个Cocoa Touch类
UITableViewController
文件。我把它命名为TableViewController

我在拖动的表视图控制器的标识检查器窗格中的自定义类下的类文本字段中键入TableViewController

我创建了一个Cocoa Touch类
UITableViewCell
文件。我把它命名为TableViewCell

class TableViewController: UITableViewController {

    let users = NSMutableArray()

    override func viewDidLoad() {

        // Register class
        self.tableView.registerClass(TableViewCell.self, forCellReuseIdnetifier: "TVC")

        // Since the Table View Controller I dragged onto Main.storyboard has outlets `dataSource` and `delegate` set to TableViewController, I do not need to set dataSource and delegate in code, correct? Either way, I've tried both ways.

        // Load data
        self.loadData()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    // Load data
    func loadData() {
        self.users.removeAllObjects()
        let query = PFQuery(className: "User")
        query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
            if error == nil {
                for person in objects! {
                    self.users.addObject(person)
                    // The print statement here is printing
                }
                self.tableView.reloadData()
            }
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let personCell = tableView.dequeueReusableCellWithIdentifier("TVC", forIndexPath: indexPath) as! EventTableViewCell
        let person = self.users.objectAtIndex(indexPath.row)
        personCell.userName.text = (event.objectForKey("email") as! String)
        return personCell
    }

所以我希望看到许多单元格,每个单元格中都有一个标签。以及基于每个单元,每个标签都以X和Y为中心


很抱歉代码墙,但我真的被难住了。

loadData函数是从示例中没有的地方调用的吗?如果不是,我想你会想做这样的事情:

override func viewDidLoad() {
    super.viewDidLoad() // <-- don't forget this part

    self.tableView.registerClass(TableViewCell.self, forCellReuseIdnetifier: "TVC")

    // Yup, your `dataSource` and `delegate` outlets are correct
    // Now load up the data
    loadData()
}

我想出来了。这些约束实际上没有被执行。我错误地认为
TableViewCell
中的
override init()
就像
viewDidLoad()
一样。所有这些代码实际上都应该移到一个新的覆盖函数中

覆盖函数布局子视图(){
//设置属性、添加到contentView、设置约束等。

}

是否设置了委托?当我将序列图像板上的表视图控制器链接到TableViewController时,委托似乎已设置。很抱歉,我忘了在示例中添加它。实际上,我在self.tableView.registerClass()上面有它,我希望这就是原因。但在把它移到下面后,它仍然不起作用。我刚刚意识到在
loadData()
的for循环中有一个print语句,我不确定它何时停止打印到控制台。但它停止了。我希望您知道您的解析,并能告诉我为什么在我的
对象数组中没有
person
对象。我在默认的
user
Parse类中有两个用户行。好了,我编辑了这个问题,并在for循环中添加了一条注释,所以它现在正在打印。现在我知道用户中有一个对象。所以我认为问题在于
覆盖func tableView()
。哦!我看到了另一件事。在UITableViewCell中的初始值设定项中,在设置自动布局约束之前,是否将标签添加到视图层次?类似这样:
self.contentView.addSubview(userName)
你说得对,我忘了在实际代码中添加它。然而,这并不能解决问题。如果没有其他人看到任何东西,我可能会以非编程方式完成这项工作。至少我现在已经大致了解了这个课程的工作原理。
override func viewDidLoad() {
    super.viewDidLoad() // <-- don't forget this part

    self.tableView.registerClass(TableViewCell.self, forCellReuseIdnetifier: "TVC")

    // Yup, your `dataSource` and `delegate` outlets are correct
    // Now load up the data
    loadData()
}
self.tableView.reloadData()