ios Swift:ViewController 中的 TableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31673607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: TableView in ViewController
提问by Eliko
I have a ViewController
in the MainstoryBoard
. I added the TableView
to it.
我ViewController
在MainstoryBoard
. 我添加了TableView
它。
MainStoryBoard:
主线板:
In addition I have an array outside of the ViewController
class and I want the objects inside the array to be shown in the TableView
.
此外,我在ViewController
类之外有一个数组,我希望数组内的对象显示在TableView
.
I can't figure out how to do it. I connected the delegate between the TableView
and the ViewController
.
我不知道该怎么做。我在TableView
和之间连接了委托ViewController
。
回答by Dharmesh Dhorajiya
You add a new table view instance variable below the class declaration.
您在类声明下方添加一个新的表视图实例变量。
@IBOutlet weak var tableView: UITableView!
To conform to the UITableViewDelegate
and UITableViewDataSource
protocol just add them separated by commas after UIViewController
in the class declaration
要符合UITableViewDelegate
和UITableViewDataSource
协议,只需UIViewController
在类声明后添加以逗号分隔的它们
After that we need to implement the tableView(_:numberOfRowsInSection:)
, tableView(_:cellForRowAtIndexPath:)
and tableView(_:didSelectRowAtIndexPath:)
methods in the ViewController
class and leave them empty for now
之后,我们需要在类中实现tableView(_:numberOfRowsInSection:)
,tableView(_:cellForRowAtIndexPath:)
和tableView(_:didSelectRowAtIndexPath:)
方法ViewController
并暂时将它们留空
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0 // your number of cells here
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// your cell coding
return UITableViewCell()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// cell selected code here
}
}
As mentioned by @ErikP in the comments, you also need to
set self.tableView.delgate = self
and self.tableView.dataSource = self
in the viewDidLoad
method (or in Storyboard works well too).
正如@ErikP 在评论中提到的,您还需要在方法中设置self.tableView.delgate = self
和(或在 Storyboard 中也能很好地工作)。self.tableView.dataSource = self
viewDidLoad
回答by Fullpower
i might be late and you may have fixed this by now. The error you are getting is due to your variable or constant returning a nil value. to test this you can assign a value to it (hard code it) and check the full code if it is working and then change it to your array, unfortunately i do stuff programmatically and not very familiar with the storyboard.
我可能迟到了,你现在可能已经解决了这个问题。您得到的错误是由于您的变量或常量返回 nil 值。要对此进行测试,您可以为其分配一个值(对其进行硬编码)并检查完整代码是否有效,然后将其更改为您的数组,不幸的是我以编程方式进行操作并且对故事板不太熟悉。
if you share your code we will be assist you if this is not yet sorted.
如果您分享您的代码,如果尚未排序,我们将为您提供帮助。