【问题标题】:Could not load NIB in Bundle for Custom Table View Cell Class无法在 Bundle 中为自定义表格视图单元格类加载 NIB
【发布时间】:2015-09-06 21:38:46
【问题描述】:

我已经被这个问题困扰了几天,我不是我的表格视图永远无法正确加载的原因。起初我的单元格返回 nil 但现在控制台状态:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/JosephHaddad/Library/Developer/CoreSimulator/Devices/2884D7B1-26CD-45F7-A044-9106962AE093/data/Containers/Bundle/Application/005E1198-CFA5-429B-960B-2367296BB6AD/Test.app> (loaded)' with name 'AllBarCell''

这是我的自定义单元格类:

 import UIKit
 import Foundation

 class AllBarCell: UITableViewCell {

// MARK: Properties


@IBOutlet weak var primaryBarImage: UIImageView?
@IBOutlet weak var barName: UILabel?
@IBOutlet weak var happyHourImage: UIImageView?
@IBOutlet weak var entImage: UIImageView?

struct Static  {
    static var data = CellDataDisplayInformation()
}


// MARK: Methods


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override func prepareForReuse() {
    self.barName?.text = nil
    super.prepareForReuse()
 }

}

还有我对应的标签表视图控制器:

     override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.tableView.registerNib(UINib(nibName: "AllBarCell", bundle: nil), forCellReuseIdentifier: "AllBarCell")
  .....




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

    let cell  = tableView.dequeueReusableCellWithIdentifier("AllBarCell", forIndexPath: indexPath) as! AllBarCell
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0)) {

        dispatch_async(dispatch_get_main_queue()) {


            if let futureCell = tableView.cellForRowAtIndexPath(indexPath) as?  AllBarCell {

                let data = AllBarCell.Static.data


                // Update Happy Hour Image


                let currentHour = GetTime()

                if data.happyStart[indexPath.row] != 999{
                    if currentHour.currentHour >= data.happyStart[indexPath.row] && currentHour.currentHour <=  data.happyEnd[indexPath.row] {
                        var barImage = UIImage(named: "HH")
                        futureCell.happyHourImage?.image = barImage
                    }
                }


                // Update Entertainment Hour Image


                if data.entStart[indexPath.row] != 999 {
                    if currentHour.currentHour >= data.entStart[indexPath.row] && currentHour.currentHour <=  data.entEnd[indexPath.row] {
                        var barImage = UIImage(named: "Ent")
                        futureCell.entImage?.image = barImage
                    }
                }


                // Update Bar Names

                if data.pictureArray[indexPath.row] != nil {
                futureCell.primaryBarImage?.image = data.pictureArray[indexPath.row]
                }

                if data.namesArray[indexPath.row] != nil {
                futureCell.barName?.text = data.namesArray[indexPath.row]
                }



            }
        }
    }

    return cell
}

如果不遇到另一个问题,我似乎无法完全解决一个问题。是的,我的自定义表格视图单元格连接到我的自定义单元格类,并且我的视图控制器也连接到正确的视图。

总结一下我的两个问题:

1) 确保我的 -cellForRowAtIndexPath 不返回零单元格

2) 避免此错误:“无法在包中加载 NIB...名称为 'AllBarsCell'”

【问题讨论】:

  • 你解决了吗?

标签: ios xcode swift uitableview


【解决方案1】:

您可能在 XCode 之外重命名了自定义单元类文件。我认为错误表明它在您的项目中找不到名为“AllBarCell”的 nib 文件。因此,请仔细检查您的班级名称。尝试从项目中删除它并重新添加它,看看会发生什么。

【讨论】:

    【解决方案2】:

    您是否在情节提要上设置了单元重用标识符?这通常是让我绊倒的原因。

    【讨论】:

    • 是的,我的单元重用标识符在情节提要上设置正确。
    【解决方案3】:

    我遇到了类似的问题,视图控制器的语法和自定义单元类都可以,并且 .xib 文件中的所有设置都很好。

    解决方案在故事板中。您应该将自己放在您的 customTableViewCell.xib 上,按 File Inspector 图标,然后在 Target Memeberhsip 中设置您的项目名称选项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2018-10-11
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多