2015-01-16 74 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。你必須檢查哪一個。 –