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

时间:2015-01-16 08:38:12

标签: ios swift tableview

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

  

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

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]()

2 个答案:

答案 0 :(得分:0)

您忘了注册课程,因此dequeueReusableCellWithIdentifier:forIndexPath:无法返回小组,例如调用

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

初始化表视图委托时。

编辑检查,您的阵列allRates已初始化并已填充。错误意味着它是空的。

答案 1 :(得分:0)

你应该确保你的&#34; allRates&#34;可以在该索引处访问数组。编写以下代码以确保它不会崩溃:

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来检查数组的状态来调试它。

相关问题