ios - 我无法在 TableView 内加载 TableView 单元格

标签 ios swift uitableview

import UIKit

class NewOrdersViewControllers: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var items = ["Chilli and Lemon"]
    @IBOutlet var tableView:UITableView!

    @IBOutlet weak var lblRestaurantNames: UILabel!
    @IBOutlet weak var mapView: UIButton!

    var cellIdentifier = "cell"

    init() {
        super.init(nibName : "NewOrdersViewControllers", bundle:nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let nib = UINib(nibName: "tableCell", bundle: nil)

        self.tableView.registerNib(nib, forCellReuseIdentifier: cellIdentifier)

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)

        tableView.delegate = self

        tableView.dataSource = self

        // Do any additional setup after loading the view.
    }

    @IBAction func mapPush(sender: AnyObject) {


        let mapVC = MapViewController()

        self.navigationController?.pushViewController(mapVC, animated: true)
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:tableCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! tableCell

        //cell.textLabel?.text = self.items[indexPath.row] as! String

        cell.RestaurantLbl.text = self.items[indexPath.row]

        return cell

    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

        print("You selected cell #\(indexPath.row)!")
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 100
    }

我使用 xib 完成了它,它显示了以下错误。

Could not cast value of type 'UITableViewCell' (0x1fe82bc)

最佳答案

在您的 viewDidLoad() 方法中,您为相同的 reuseIdentifier 注册了 2 个不同的单元格类型,因此最后一个 (UITableViewCell) 生效.这就是为什么在 tableView:cellForRowAtIndexPath: 中出队的单元格也是 UITableViewCell 类,可能不会转换为您的自定义单元格类。

尝试删除该行:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)

它应该可以解决您的问题并让 tableView 出列正确的单元格。

关于ios - 我无法在 TableView 内加载 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493111/

相关文章:

ios - 更改标题 View 的背景颜色

ios - Storyboard和 ViewController 工厂

ios - 在 iOS 应用程序 (Swift) 中使用 telegram 分享消息

swift - 为什么我的 tableView 没有推送到 tableView 内的详细信息 ViewController?

swift - 如何在 Swift FMDB 中使用executeQuery() 执行基于条件的查询

swift - 当表格 View 单元格发生变化时如何更新它

android - 在 Firebase 中启用持久性时实际发生了什么?

ios - 如何在 ios 中的 CALayer 上添加按钮?

ios - 在快速递归插入和更新数据库中的数据时面临内存问题

objective-c - 发送到实例 UIViewController 的无法识别的选择器