【发布时间】: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