ios 快速在tableview中显示数组项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29011595/
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 05:10:49  来源:igfitidea点击:

displaying array items in tableview with swift

iosuitableviewswift

提问by Timur Aykut YILDIRIM

I cannot display elements of an array in a tableview by using swift programming language. I did some research but could not solve the problem. Below, you can see my code and download project as a .zip file from here: http://1drv.ms/1Ca2hyp

我无法使用 swift 编程语言在 tableview 中显示数组的元素。我做了一些研究,但无法解决问题。在下面,您可以从此处查看我的代码并将项目下载为 .zip 文件:http: //1drv.ms/1Ca2hyp

I can also give some videos. They're 11 min and 10 sec. long in total

我也可以提供一些视频。他们是 11 分 10 秒。总长

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var myTableView: UITableView!
    var cars = [String]()
    var newCar: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        cars = ["BMW","Audi", "Volkswagen"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cars.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = cars[indexPath.row]

        return cell
    }

}

回答by Nikita Kukushkin

You forgot to register your controller as a dataSource for UITableView.
Add this to your code:

您忘记将控制器注册为 UITableView 的数据源。
将此添加到您的代码中:

@IBOutlet var myTableView: UITableView! {
    didSet {
        myTableView.dataSource = self
    }
}

回答by Devendra Agnihotri

Maybe you forgot to add these two lines to your view controller's viewDidLoadmethod:

也许您忘记将这两行添加到您的视图控制器的viewDidLoad方法中:

yourTableView.delegate = self

yourTableView.dataSource = self

回答by Ramprasath Selvam

    class SelectCategoryViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
        var CategeryArray = ["Food","Travel","Lifestyle","Card","MyReserve","Game","Songs","Movies","Entertainment","Business","Education","Finance","Drink","Sports","Social","Shopping"]



    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            self.tableView.dataSource = self
            self.tableView.delegate = self
        }
}
    extension SelectCategoryViewController: UITableViewDelegate,UITableViewDataSource {

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return CategeryArray.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell")!
            cell.textLabel?.text = self.CategeryArray[indexPath.row]
            return cell
        }
    }

set the identifier =TableViewCellin Main.storyboard
And also set the class name for TableViewCell

在 Main.storyboard 中设置 identifier = TableViewCell
并为TableViewCell设置类名