繁体   English   中英

Swift-应用程序在TableView UIRefreshControl上崩溃

[英]Swift - app crashes on TableView UIRefreshControl

当我开始更新表视图(下拉以刷新),然后突然开始翻转列表时,应用程序崩溃。

致命错误:无法索引空缓冲区

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("BankCell", forIndexPath: indexPath) as BankTableViewCell

    cell.backgroundColor = UIColor(red: 241/255, green: 233/255, blue: 220/255, alpha: 1.0)

    let bank:Bank = self.allRates[indexPath.row] as Bank // <-- Error here

    cell.titleLabel.text = bank.name

    return cell
}

可能我必须检查数组中元素的存在。 但这是正确的出路吗?

-我编辑第2行:

let cell = tableView.dequeueReusableCellWithIdentifier("BankCell") as BankTableViewCell

但是错误仍然存​​在。

我的刷新功能:

func refresh(sender:AnyObject)
{

    parser.deleteObjects()
    self.allRates.removeAll(keepCapacity: false)

    parser.parse { // - XMLParser ended to Parse file

        self.allRates = self.parser.actualBankRates + self.parser.notActualBankRates

        self.tableView.reloadData()

        self.refreshController.endRefreshing()
    }
}

在XMLParser中:

var actualBankRates = [Bank]()
var notActualBankRates = [Bank]()

您忘记注册类,因此dequeueReusableCellWithIdentifier:forIndexPath:无法返回单元格,例如调用

tableView.registerClass(BankCell.classForCoder(), forCellReuseIdentifier: "BankCell")

初始化表视图委托时。

编辑检查,以确保数组allRates已初始化并填充。 该错误表示它为空。

您应该确保可以在该索引处访问“ allRates”数组。 编写以下代码,以确保它不会崩溃:

if self.allRates.count > indexPath.row
{
    // Ensure you get a valid Bank returned
    if let bank = self.allRates[indexPath.row] as Bank
    {
        cell.titleLabel.text = bank.name
    }
}

然后,您可以通过在第一个if语句上插入断点并键入po self.allRates来调试它,然后再尝试访问它的状态来进行调试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM