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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:01:52  来源:igfitidea点击:

Swift: TableView in ViewController

iosiphoneswiftxcodeuitableview

提问by Eliko

I have a ViewControllerin the MainstoryBoard. I added the TableViewto it.

ViewControllerMainstoryBoard. 我添加了TableView它。

MainStoryBoard:

主线板:

enter image description here

在此处输入图片说明

In addition I have an array outside of the ViewControllerclass 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 TableViewand 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 UITableViewDelegateand UITableViewDataSourceprotocol just add them separated by commas after UIViewControllerin the class declaration

要符合UITableViewDelegateUITableViewDataSource协议,只需UIViewController在类声明后添加以逗号分隔的它们

After that we need to implement the tableView(_:numberOfRowsInSection:), tableView(_:cellForRowAtIndexPath:)and tableView(_:didSelectRowAtIndexPath:)methods in the ViewControllerclass 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 = selfand self.tableView.dataSource = selfin the viewDidLoadmethod (or in Storyboard works well too).

正如@ErikP 在评论中提到的,您还需要在方法中设置self.tableView.delgate = self和(或在 Storyboard 中也能很好地工作)。self.tableView.dataSource = selfviewDidLoad

回答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.

如果您分享您的代码,如果尚未排序,我们将为您提供帮助。