Xcode simulator table view is black

人盡茶涼 提交于 2019-12-12 20:25:46

问题


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

Here is an image of my simulator:

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:

Main.storyboard:

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:


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.

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.)

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.



来源:https://stackoverflow.com/questions/39300033/xcode-simulator-table-view-is-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!