【问题标题】:displaying array items in tableview with swift用swift在tableview中显示数组项
【发布时间】:2015-05-14 17:12:22
【问题描述】:

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

我也可以提供一些视频。它们是 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
    }

}

【问题讨论】:

  • 运行应用时会发生什么?您是否收到错误消息,或者您只是在屏幕上看不到任何内容?您是否确保将 Storyboard 或 xib 文件中单元格的单元格标识符设置为“myCell”?
  • 故事板上的@Alexander 表格单元格具有与“myCell”相同的标识符。当我运行代码时,它没有给出错误

标签: ios uitableview swift


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    yourTableView.delegate = self
    
    yourTableView.dataSource = self
    

    【讨论】:

      【解决方案3】:
          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
              }
          }
      

      在 Main.storyboard
      中设置标识符 =TableViewCell 并为 TableViewCell

      设置类名

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-24
        • 2020-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多