2015-01-16 75 views
0

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

fatal error: Cannot index empty buffer

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

在下拉刷新并重新载入更新的数据后会崩溃吗? – rakeshbs

回答

0

您应该确保您的“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检查您的阵列的状态你试图访问它之前对其进行调试。

+0

工作,但其他操作员呢?我必须返回空单元格? – Mazorati

0

你忘了注册你的类,因此dequeueReusableCellWithIdentifier:forIndexPath:是无法返回的细胞,如致电

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

初始化您的表视图委托。

编辑检查,你的数组allRates被初始化和填充。错误意味着它是空的。

+0

致命错误:在解包可选值时意外发现nil - in cell.titleLabel.text = bank.name – Mazorati

+0

这些是可选和nil。你必须检查哪一个。 –