I've been at this for almost two days. I cannot get it to display anything. I am going to go step by step because I have no idea what the problem is.
I start with an empty Main.storyboard.
I drag a Table View Controller to the Main.storyboard.
I embed the Table View Controller in a Navigation Controller.
I create a Cocoa Touch Class UITableViewController
file. I name it TableViewController.
I type TableViewController in the Class text field under Custom Class in the identity inspector pane of the dragged Table View Controller.
I create a Cocoa Touch Class UITableViewCell
file. I name it 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
}
.
class TableViewCell: UITableViewCell {
let userName = UILabel()
// I'm assuming the reuseIdentifier below should be the same as the one in TableViewController
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: "TVC")
userName.text = PFUser.currentUser()!.email
userName.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(userName)
self.contentView.addConstraint(NSLayoutConstraint(item: UserName,
attribute: .CenterX,
relatedBy: .Equal,
toItem: self.contentView,
attribute: .CenterX,
multiplier: 1,
constant: 0))
self.contentView.addConstraint(NSLayoutConstraint(item: userName,
attribute: .CenterY,
relatedBy: .Equal,
toItem: self.contentView,
attribute: .CenterY,
multiplier: 1,
constant: 0))
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
So I expect to see a number of cells with one label in each cell. And each label being centred on both X and Y based on each cell.
Sorry for the wall of code, but I am really stumped.