为什么我在 Swift 的 UITableView 中看不到任何单元格?
why I don't see any cells in my UITableView in Swift?
我有一个 UIViewController
我戴上了它 TableView
:
我还向其中添加了 UITableViewCell
,在情节提要中检查了我的 table 视图具有动态生成的内容,并向 UITableViewCell
添加了一个标识符 "cell" .我将它与插座连接到我的 class 并设置了从 UITableViewDelegate
:
的继承
class MapViewController: UIViewController, UITableViewDelegate, UITextViewDelegate {
@IBOutlet weak var myTableDetailsPanel: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myTableDetailsPanel.delegate = self
}
然后我将我的自定义对象添加到我的数组中:
var items = NSMutableArray()
...
print("size of items before: \(items.count)")
items.addObject(myObject)
print("size of items after: \(items.count)")
myTableDetailsPanel.reloadData()
myTableDetailsPanel.hidden = false
我也有这个方法:
func tableView(myTableDetailsPanel: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableDetailsPanel.dequeueReusableCellWithIdentifier("cell") as! MyDetailsCell
let yawp:MySingleObject = self.items[indexPath.row] as! MySingleObject
print("hello")
...
而且...我的问题是面板显示为空,在控制台中我看到:
size of items before: 0
size of items after: 1
但不知何故我没有看到 hello
。我来自 cellForRowAtIndexPath
的代码从未运行过。为什么会这样?
当您实施 UITableViewDelegate
协议以通知有关 UITableView
的更改时,您必须实施 UITableViewDataSource
,数据源提供给 table-view具有构建和修改 table 视图所需信息的对象。
所以你需要实现 UITableViewDataSource
协议并将 dataSource
声明为 self
,如下所示:
myTableDetailsPanel.dataSource = self
希望对你有所帮助。
我有一个 UIViewController
我戴上了它 TableView
:
我还向其中添加了 UITableViewCell
,在情节提要中检查了我的 table 视图具有动态生成的内容,并向 UITableViewCell
添加了一个标识符 "cell" .我将它与插座连接到我的 class 并设置了从 UITableViewDelegate
:
class MapViewController: UIViewController, UITableViewDelegate, UITextViewDelegate {
@IBOutlet weak var myTableDetailsPanel: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myTableDetailsPanel.delegate = self
}
然后我将我的自定义对象添加到我的数组中:
var items = NSMutableArray()
...
print("size of items before: \(items.count)")
items.addObject(myObject)
print("size of items after: \(items.count)")
myTableDetailsPanel.reloadData()
myTableDetailsPanel.hidden = false
我也有这个方法:
func tableView(myTableDetailsPanel: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableDetailsPanel.dequeueReusableCellWithIdentifier("cell") as! MyDetailsCell
let yawp:MySingleObject = self.items[indexPath.row] as! MySingleObject
print("hello")
...
而且...我的问题是面板显示为空,在控制台中我看到:
size of items before: 0
size of items after: 1
但不知何故我没有看到 hello
。我来自 cellForRowAtIndexPath
的代码从未运行过。为什么会这样?
当您实施 UITableViewDelegate
协议以通知有关 UITableView
的更改时,您必须实施 UITableViewDataSource
,数据源提供给 table-view具有构建和修改 table 视图所需信息的对象。
所以你需要实现 UITableViewDataSource
协议并将 dataSource
声明为 self
,如下所示:
myTableDetailsPanel.dataSource = self
希望对你有所帮助。