我之前按照教程学习了 Storyboard 的基础知识,我正在使用该代码作为引用来编写我正在开发的应用程序。我想测试我的原型(prototype)单元格布局,但即使我在 viewDidLoad 为数组设置了一个值,它仍然拒绝显示任何内容。
import UIKit
class DetailViewController: UIViewController {
var cards = [Card]()
@IBOutlet weak var cardsTableView: UITableView!
@IBOutlet weak var detailDescriptionLabel: UILabel!
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail: AnyObject = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.description
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cards.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//példányosítunk egy cellát
let cell = tableView.dequeueReusableCellWithIdentifier("CardCell", forIndexPath: indexPath) as CardCell
//kivesszük a sor adatait a listából
let card : Card = self.cards[indexPath.row]
cell.setCardNumber(card.number)
cell.layer.cornerRadius = 8
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
self.cards = [Card(number: 123456789,type: 1)]
dispatch_async(dispatch_get_main_queue(), {
self.cardsTableView.reloadData()
})
self.configureView()
}
}
我从一个Master-Detail结构开始,我将Detail Scene的类设置为DetailViewController,将Prototype的类和标识符设置为CardCell
class Card{
let number: Int
let type: Int
init(number: Int, type: Int){
self.number = number
self.type = type
}
}
import UIKit
class CardCell: UITableViewCell {
@IBOutlet weak var cardNumber: UILabel!
func setCardNumber(number: Int){
cardNumber.text = String(number)
}
}
我确定这是一些基本的东西,但我已经连续第二天搞砸了。任何建议表示赞赏。
最佳答案
你必须为 TableView 设置数据源,我猜委托(delegate)方法永远不会被调用。
另外添加 UITableViewDataSource,这不是必需的,但比实现更具声明性
class DetailViewController: UIViewController, UITableViewDataSource {
关于iOS Swift Dequeued Reusable Cell 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695553/