iOS Swift Dequeued Reusable Cell 未显示

标签 ios uitableview swift

我之前按照教程学习了 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/

相关文章:

ios - 添加新单元格时,UItableview 上一个单元格消失

ios - 如果滚动 UITableView 时出现 popViewController,应用程序崩溃

ios - Swift 将 JSON 解析为模型对象

ios - 如何使用 Swift 模糊 UIImageView 中的现有图像?

objective-c - Objective-C : Compare two enum types not working

iOS Swift 1.2 - 原始类型比较失败

ios - 更改 UILabel 文本仅适用于 viewDidLoad()

ios - 在查询解析表之前获取位置查询

ios - 将 UISearchBar returnKeyType 更改为 Done 在 iPhone 5 上有效,但在 iPhone 4s 上会使应用程序崩溃

ios - 'YearCalendarUnit' 在 OS X 版本 10.10 : Use NSCalendarUnitYear instead 中被弃用