Xcode 模拟器 table 视图是黑色的
Xcode simulator table view is black
当运行我的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 的 class。您会在 table 视图的属性检查器中找到用于键入 class 名称的输入字段。
2) 检查您是否实现了符合 UITableViewDelegate 和 UITableViewDataSource 协议的方法。
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) 检查您的 table 是否从 Storyboard 正确连接到您的 UIViewController =>
由于您的 tableView 在 UIViewController 中,请检查您是否在控制器中为 tableView 设置了委托和数据源(CTRL 从 table 拖动到 FileOwner - 中的圆形黄色图标故事板场景框架。)
4) 确保您设置了 UIViewController 应遵守的 协议 - 即委托和数据源协议:
class MyPinpointsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ...
5) 检查您的数据源,因为它可能不会在 cellForRow()
方法中返回任何项目。它可以在 print(dataSource)
中的任何 TableView 委托方法中进行简单测试。
6) 检查可能的自动布局问题,因为重要的子视图可能在视图的可见部分之外。
当运行我的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 的 class。您会在 table 视图的属性检查器中找到用于键入 class 名称的输入字段。
2) 检查您是否实现了符合 UITableViewDelegate 和 UITableViewDataSource 协议的方法。
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) 检查您的 table 是否从 Storyboard 正确连接到您的 UIViewController =>
由于您的 tableView 在 UIViewController 中,请检查您是否在控制器中为 tableView 设置了委托和数据源(CTRL 从 table 拖动到 FileOwner - 中的圆形黄色图标故事板场景框架。)
4) 确保您设置了 UIViewController 应遵守的 协议 - 即委托和数据源协议:
class MyPinpointsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ...
5) 检查您的数据源,因为它可能不会在 cellForRow()
方法中返回任何项目。它可以在 print(dataSource)
中的任何 TableView 委托方法中进行简单测试。
6) 检查可能的自动布局问题,因为重要的子视图可能在视图的可见部分之外。