Xcode simulator table view is black

2019-07-09 07:24发布

When running my Xcode project in the simulator, my UITableView in my UIViewController (not UITableViewController) is black.

Here is an image of my simulator:

enter image description here

Code for my cellForRowAtIndexPath method:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyPinpointsTableViewCell

    // Configure the cell...
    cell.titleLabel.text = myPinpoints[indexPath.row].title
    cell.locationLabel.text = myPinpoints[indexPath.row].location
    cell.dateLabel.text = myPinpoints[indexPath.row].date
    cell.pinpointImage.image = UIImage(data: myPinpoints[indexPath.row].image) 

    return cell
}

Here are the folders that I have:

enter image description here

Main.storyboard:

enter image description here

The error message I get when I add print(myPinpoints) to numberOfRowsInSection

[ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )] [ (entity: Details; id: 0xd000000000080000 ; data: )]

1条回答
▲ chillily
2楼-- · 2019-07-09 07:43

You may miss some stuff:

1) Check whether your tableView has set the class named as that on in your code. You find the input field to type the name of the class in Attributes inspector of the tableView. enter image description here

2) Check whether you implemented methods, that comform to UITableViewDelegate and UITableViewDataSource Protocol.

func numberOfSections(in tableView: UITableView) -> Int {
    // I see you only have 1 section now
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//you should return appropriate number
   return 3
}

3) Check whether your table is properly connected from Storyboard to your UIViewController =>

As your tableView is inside UIViewController, check whether you set delegate and datasource for tableView in your controller (CTRL drag from table to FileOwner - rounded yellow icon in storyboard scene frame.)

enter image description here

4) Make sure, you set protocols that your UIViewController should conform to - i.e. delegate and dataSource protocols:

class MyPinpointsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ... 

5) Check your datasource, as it may not be returning any items in cellForRow() method. It can by simply tested in print(dataSource) in any of TableView's delegate methods.

6) Check possible autolayout issues, as the important subviews could be out of visible part of the view.

查看更多
登录 后发表回答