ios - Xcode 模拟器 TableView 是黑色的

标签 ios swift uitableview ios-simulator

在模拟器中运行我的 Xcode 项目时,我的 UIViewController(不是 UITableViewController)中的 UITableView 是黑色的。

这是我的模拟器的图片:

enter image description here

我的 cellForRowAtIndexPath 方法的代码:

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
}

这是我的文件夹:

enter image description here

Main.storyboard:

enter image description here

print(myPinpoints) 添加到 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) 检查您的tableView是否在您的代码中设置了名为on的类。您会在 tableView 的属性检查器中找到用于键入类名称的输入字段。 enter image description here

2)检查您是否实现了符合 UITableViewDelegate 和 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) 检查您的表格是否已从 Storyboard 正确连接到您的 UIViewController =>

由于您的 tableView 在 UIViewController 中,请检查您是否在 Controller 中为 tableView 设置了委托(delegate)和数据源(CTRL 从表拖动到 FileOwner - Storyboard场景框架中的圆形黄色图标。)

enter image description here

4) 确保您设置了 UIViewController 应遵守的协议(protocol) - 即委托(delegate)和数据源协议(protocol):

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

5) 检查您的数据源,因为它可能没有在 cellForRow() 方法中返回任何项目。它可以在任何 TableView 的委托(delegate)方法中的 print(dataSource) 中简单地进行测试。

6) 检查可能的自动布局问题,因为重要的 subview 可能不在 View 的可见部分。

关于ios - Xcode 模拟器 TableView 是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39300033/

相关文章:

ios - 在 UITableView 中保持时间戳标签更新

iphone - 如何在 UITableView 下方添加事件指示器?

ios - 如何使用Swift 5从相册获得图像的URL

ios - 从 UITableViewCell 禁用 subview isHighlighted

swift - scrolltoitematindexpath 总是返回到相同的位置

ios - 在navigationBar中设置rightBarButtonItem

ios - UITableView 单元格中的粗体字体

iphone - 当线程在保存时被杀死时,如何防止 NSManagedObjectContext 破坏数据库?

ios - RestKit是否在不同线程上执行请求?

ios - 在模拟器和本地设备上获取不同的时间格式 - Swift iOS