Ios dequeReusableCellWithIdentifier上的UITableView崩溃

Ios dequeReusableCellWithIdentifier上的UITableView崩溃,ios,uitableview,swift,Ios,Uitableview,Swift,嗯,好的。也许我盯着屏幕看的时间太长了 我不理解这种奇怪的崩溃(我知道错误是怎么说的,但我甚至不知道它是怎么发生的) 我的应用程序中基本上有两个UITableView并排,当它试图将一个单元格出列时,应用程序正在崩溃 我的代码如下: func initView() { ... // init shop table view self.tblShop = UITableView(frame: CGRectMake(20.0, self.lblShop!.frame.ori

嗯,好的。也许我盯着屏幕看的时间太长了

我不理解这种奇怪的崩溃(我知道错误是怎么说的,但我甚至不知道它是怎么发生的)

我的应用程序中基本上有两个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) 
我注意到一件非常奇怪的事情:

My
numberOfRowsInSection
函数显示:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(tableView == self.tblShop)
    {
        return 5;
    }
    else
    {
        return 2;
    }
}
为什么我的控制台日志记录了5批“inventoryCell”而不是“shopCell”,为什么控制台日志记录了3批“shopCell”


好吧,我盯着屏幕看太久了

我在我的类中声明了这两个方法:

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


我犯了一个愚蠢的错误,很抱歉浪费您的时间阅读我的问题lol。

除了这里显示的方法之外,您还有其他表数据源方法吗?几节?这可能也会有帮助:我有两个构造函数,它们都调用了
self.initViews()
方法,这导致了TableView的双重初始化,导致崩溃(请参见下面我自己的答案)。没问题,很高兴您能解决。
self.initViews();