我尝试在taleview中使用我自己的单元格,但是出现以下错误。我检查了各种样本(也在stackoverflow中),甚至没有设法让它运行。有人能看出我做错了吗?
错误:
2015-05-10 07:55:53.145 Prototypes[915:19191] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/user/Library/Developer/CoreSimulator/Devices/FE514C52-DD27-4114-A51F-2A837A6E9CF2/data/Containers/Bundle/Application/9AA69B81-4001-4E95-8DD8-2D0B742B175A/Prototypes.app> (loaded)' with name 'ResultsCell''
代码: UIViewController中: var searchResults:UITableView = UITableView();
override func viewDidLoad() {
let data = (EventID: 0, sTitle: "MyEvent 0");
arrayResults.append(data);
let fSearchResultY:CGFloat=fButtonHeight+fButtonY;
searchResults.frame = CGRectMake(0, fSearchResultY, screenwidth, screenheight-fSearchResultY);
searchResults.delegate = self;
searchResults.dataSource = self;
let nibName = UINib(nibName: "ResultsCell", bundle:nil)
searchResults.registerNib(nibName, forCellReuseIdentifier: "Cell")
self.view.addSubview(searchResults);
}
的UITableViewCell:
import UIKit
class ResultsCell: UITableViewCell {
var cellImage: UIImageView
var cellTitle: UILabel
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
cellImage=UIImageView();
cellTitle=UILabel();
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
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
}
}
建议后更新:
我替换了
let nibName = UINib(nibName: "ResultsCell", bundle:nil)
searchResults.registerNib(nibName, forCellReuseIdentifier:
&#34;细胞&#34) 与
searchResults.registerClass(ResultsCell.self, forCellReuseIdentifier: "cell");
但后来我得到了:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
答案 0 :(得分:1)
'Could not load NIB in bundle: 'NSBundle' with name 'ResultsCell''
Code: UIViewController: var searchResults: UITableView = UITableView();
表示iOS无法在您的捆绑包中找到名称为ResultsCell
的NIB / XIB。确保您拥有该XIB文件,或者如果您想逐个代码地创建表格视图,则应使用-[UITableView registerClass: forCellReuseIdentifier:]
代替