Swift UITableView 没有显示内容
Swift UITableView does not show up the content
我有一个侧面菜单,我想为每个选择加载一个新的 ViewController(基本上每个页面都有一个单独的 ViewController)。
这是我加载新 ViewController:
的相关代码
viewController = storyboard!.instantiateViewController(withIdentifier: "commissionsViewController")
var viewInAction = UIView()
viewInAction = viewController.view
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapOnView))
viewInAction.addGestureRecognizer(gestureRecognizer)
viewInAction.layer.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).cgColor
viewInAction.translatesAutoresizingMaskIntoConstraints = false
if let subview = containerView.viewWithTag(100) {
subview.removeFromSuperview()
}
containerView.insertSubview(viewInAction, belowSubview: menuView)
setConstrainsOnView(viewObject: viewInAction)
这是加载的 ViewController 的代码:
import UIKit
class CommissionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
var tableData = ["Beach", "Clubs", "Chill", "Dance"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my")
tableView.contentInset.top = 20
let contentSize = self.tableView.contentSize
let footer = UIView(frame: CGRect(x: self.tableView.frame.origin.x,
y: self.tableView.frame.origin.y + contentSize.height,
width: self.tableView.frame.size.width,
height: self.tableView.frame.height - self.tableView.contentSize.height))
self.tableView.tableFooterView = footer
view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath)
cell.textLabel?.text = "This is row \(tableData[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
问题: 如您所见,CommissionsViewController 包含一个以编程方式添加的 tableView。该 tableView 没有显示任何单元格!基本上方法 cellForRowAt 永远不会被调用!我不明白为什么!
我可以看到一个蓝色视图,它是 tableView 的背景,所以 tableView 就位了,但它没有加载单元格!
调用方法 numberOfRowsInSection。
我尝试使用调试器对其进行调试,但没有结果。根本不懂流程
恭敬地感谢您。
设置dataSource 并将其委托给self 后,您永远不会让tableView 加载任何数据。这意味着您需要在 viewDidLoad()
结束时调用 tableView.reloadData()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my")
tableView.contentInset.top = 20
let contentSize = self.tableView.contentSize
let footer = UIView(frame: CGRect(x: self.tableView.frame.origin.x,
y: self.tableView.frame.origin.y + contentSize.height,
width: self.tableView.frame.size.width,
height: self.tableView.frame.height - self.tableView.contentSize.height))
self.tableView.tableFooterView = footer
view.addSubview(tableView)
tableView.reloadData()
}
当您将另一个 viewController 的视图添加到您的视图层次结构时,您必须告知两个 viewController(有关详细信息,请参阅 )。
像这样添加 addChildViewController
和 didMoveToParentViewController
调用:
viewController = storyboard!.instantiateViewController(withIdentifier: "commissionsViewController")
self.addChildViewController(viewController)
var viewInAction = viewController.view
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapOnView))
viewInAction.addGestureRecognizer(gestureRecognizer)
viewInAction.layer.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).cgColor
viewInAction.translatesAutoresizingMaskIntoConstraints = false
if let subview = containerView.viewWithTag(100) {
subview.removeFromSuperview()
}
containerView.insertSubview(viewInAction, belowSubview: menuView)
setConstrainsOnView(viewObject: viewInAction)
viewController.didMoveToParentViewController(self)
在 viewDidLoad
中加载方法 view
但大小不正确。
尝试用下一种方式更改 UITableView 的初始化
let screenBounds = UIScreen.main.bounds
let rect = CGRect(x: 0, y: 0, width: screenBounds.width, height: screenBounds.height)
tableView = UITableView(frame: rect, style: UITableViewStyle.plain)
转到ComissionsViewController
、viewDidLoad()
方法并在view.addSubview(tableView)
之后添加tableView.reloadData()
——它会让你所有的烦恼都消失。
UPD:我刚刚制作了一个可以正常工作的测试项目。我复制了你发布的代码,只评论了几行不相关的内容。我所做的唯一重要区别是添加了上面提到的代码行。您可以下载并 运行 看看它是否有效 :)
https://drive.google.com/file/d/0B55tO8S_7Ag7VnppMk1fWlg5dWM/view
我有一个侧面菜单,我想为每个选择加载一个新的 ViewController(基本上每个页面都有一个单独的 ViewController)。
这是我加载新 ViewController:
的相关代码 viewController = storyboard!.instantiateViewController(withIdentifier: "commissionsViewController")
var viewInAction = UIView()
viewInAction = viewController.view
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapOnView))
viewInAction.addGestureRecognizer(gestureRecognizer)
viewInAction.layer.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).cgColor
viewInAction.translatesAutoresizingMaskIntoConstraints = false
if let subview = containerView.viewWithTag(100) {
subview.removeFromSuperview()
}
containerView.insertSubview(viewInAction, belowSubview: menuView)
setConstrainsOnView(viewObject: viewInAction)
这是加载的 ViewController 的代码:
import UIKit
class CommissionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
var tableData = ["Beach", "Clubs", "Chill", "Dance"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my")
tableView.contentInset.top = 20
let contentSize = self.tableView.contentSize
let footer = UIView(frame: CGRect(x: self.tableView.frame.origin.x,
y: self.tableView.frame.origin.y + contentSize.height,
width: self.tableView.frame.size.width,
height: self.tableView.frame.height - self.tableView.contentSize.height))
self.tableView.tableFooterView = footer
view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath)
cell.textLabel?.text = "This is row \(tableData[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
问题: 如您所见,CommissionsViewController 包含一个以编程方式添加的 tableView。该 tableView 没有显示任何单元格!基本上方法 cellForRowAt 永远不会被调用!我不明白为什么!
我可以看到一个蓝色视图,它是 tableView 的背景,所以 tableView 就位了,但它没有加载单元格!
调用方法 numberOfRowsInSection。
我尝试使用调试器对其进行调试,但没有结果。根本不懂流程
恭敬地感谢您。
设置dataSource 并将其委托给self 后,您永远不会让tableView 加载任何数据。这意味着您需要在 viewDidLoad()
结束时调用 tableView.reloadData()override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my")
tableView.contentInset.top = 20
let contentSize = self.tableView.contentSize
let footer = UIView(frame: CGRect(x: self.tableView.frame.origin.x,
y: self.tableView.frame.origin.y + contentSize.height,
width: self.tableView.frame.size.width,
height: self.tableView.frame.height - self.tableView.contentSize.height))
self.tableView.tableFooterView = footer
view.addSubview(tableView)
tableView.reloadData()
}
当您将另一个 viewController 的视图添加到您的视图层次结构时,您必须告知两个 viewController(有关详细信息,请参阅
像这样添加 addChildViewController
和 didMoveToParentViewController
调用:
viewController = storyboard!.instantiateViewController(withIdentifier: "commissionsViewController")
self.addChildViewController(viewController)
var viewInAction = viewController.view
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapOnView))
viewInAction.addGestureRecognizer(gestureRecognizer)
viewInAction.layer.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).cgColor
viewInAction.translatesAutoresizingMaskIntoConstraints = false
if let subview = containerView.viewWithTag(100) {
subview.removeFromSuperview()
}
containerView.insertSubview(viewInAction, belowSubview: menuView)
setConstrainsOnView(viewObject: viewInAction)
viewController.didMoveToParentViewController(self)
在 viewDidLoad
中加载方法 view
但大小不正确。
尝试用下一种方式更改 UITableView 的初始化
let screenBounds = UIScreen.main.bounds
let rect = CGRect(x: 0, y: 0, width: screenBounds.width, height: screenBounds.height)
tableView = UITableView(frame: rect, style: UITableViewStyle.plain)
转到ComissionsViewController
、viewDidLoad()
方法并在view.addSubview(tableView)
之后添加tableView.reloadData()
——它会让你所有的烦恼都消失。
UPD:我刚刚制作了一个可以正常工作的测试项目。我复制了你发布的代码,只评论了几行不相关的内容。我所做的唯一重要区别是添加了上面提到的代码行。您可以下载并 运行 看看它是否有效 :) https://drive.google.com/file/d/0B55tO8S_7Ag7VnppMk1fWlg5dWM/view