在模拟器中运行我的 Xcode 项目时,我的 UIViewController
(不是 UITableViewController
)中的 UITableView
是黑色的。
这是我的模拟器的图片:
我的 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
}
这是我的文件夹:
Main.storyboard
:
将 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 的属性检查器中找到用于键入类名称的输入字段。
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场景框架中的圆形黄色图标。)
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/