我不明白这种奇怪的崩溃(我知道错误在说什么,但我甚至不知道它是如何发生的。)
我在我的应用程序中基本上有两个UITableView并且当应用程序尝试将单元格出列时,应用程序崩溃。
我的代码是这样的:
func initView()
{
...
// init shop table view
self.tblShop = UITableView(frame: CGRectMake(20.0, self.lblShop!.frame.origin.y + self.lblShop!.frame.size.height, 150.0, 150.0), style: UITableViewStyle.Plain);
self.tblShop?.rowHeight = 60.0;
self.tblShop?.delegate = self;
self.tblShop?.dataSource = self;
self.tblShop?.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "shopCell");
self.tblShop?.showsVerticalScrollIndicator = false;
self.tblShop?.showsHorizontalScrollIndicator = false;
// init bank table view
self.tblInventory = UITableView(frame: CGRectMake(self.tblShop!.frame.origin.x + self.tblShop!.frame.size.width + 20.0, self.tblShop!.frame.origin.y, self.tblShop!.frame.size.width, self.tblShop!.frame.size.height));
self.tblInventory?.rowHeight = self.tblShop!.rowHeight;
self.tblInventory?.delegate = self;
self.tblInventory?.dataSource = self;
self.tblInventory?.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "inventoryCell");
self.tblInventory?.showsVerticalScrollIndicator = false;
self.tblInventory?.showsHorizontalScrollIndicator = false;
self.addSubview(self.lblShop!);
self.addSubview(self.lblBank!);
self.addSubview(self.tblShop!);
self.addSubview(self.tblInventory!);
}
// ----------------------------------------------------------------------------
// MARK: - TableView Delegate Methods -
// ----------------------------------------------------------------------------
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(tableView == self.tblShop)
{
return 5;
}
else
{
return 2;
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellID:NSString?;
if(tableView == self.tblShop)
{
cellID = "shopCell";
}
else
{
cellID = "inventoryCell";
}
println("cell type = \(cellID!)");
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID!, forIndexPath: indexPath) as UITableViewCell;
if(tableView == self.tblShop)
{
cell.textLabel.text = "Warm hat";
}
else
{
cell.textLabel.text = "Viking hat";
}
return cell;
}
这是我的Xcode控制台输出:
Showing bank view
(lldb) po tableView == self.tblShop
true
cell type = inventoryCell
cell type = inventoryCell
cell type = shopCell
cell type = shopCell
cell type = shopCell
cell type = inventoryCell
cell type = inventoryCell
cell type = inventoryCell
2014-11-29 21:26:11.592 MyApp[1740:357230] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-3318.16.21/UITableView.m:6116
2014-11-29 21:26:11.595 MyApp[1740:357230] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier inventoryCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(0x21bed49f 0x2f3a3c8b 0x21bed375 0x228bed7f 0x2521282d 0x9ec80 0x9f6e8 0x25375787 0x2537584b 0x2536afa1 0x251830df 0x250ad24f 0x24ad5a0d 0x24ad13e5 0x24ad126d 0x24ad0c51 0x24ad0a55 0x250aec13 0x21bb3d57 0x21bb3167 0x21bb17cd 0x21aff3c1 0x21aff1d3 0x28efd0a9 0x2510efa1 0xa8728 0xa8764 0x2f923aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我注意到一件非常奇怪的事情:
我的numberOfRowsInSection
功能显示:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(tableView == self.tblShop)
{
return 5;
}
else
{
return 2;
}
}
为什么我的控制台日志会产生5批" inventoryCell"而不是" shopCell"为什么控制台会记录3个" shopCell" ?:
cell type = inventoryCell
cell type = inventoryCell
cell type = shopCell
cell type = shopCell
cell type = shopCell
cell type = inventoryCell
cell type = inventoryCell
cell type = inventoryCell
答案 0 :(得分:0)
好的,我一直盯着屏幕看太久了。
我在班上宣布了这两种方法:
override init() {
super.init();
self.initViews();
}
override init(frame: CGRect) {
super.init(frame: frame);
self.initViews();
}
所以,当我使用:
初始化我的自定义视图时self.customView = CustomView();
它又执行了方法:
override init() {
super.init();
self.initViews();
}
本质上称initWithFrame
传递CGRectZero
:
override init(frame: CGRect) {
super.init(frame: frame);
self.initViews();
}
所以,我最终执行:
self.initViews();
两次,它分配了两次tableView,可能导致崩溃,因为第一次没有在第二次启动之前完成alloc。
:d
我的愚蠢错误,抱歉浪费你的时间阅读我的问题大声笑。