2017-02-27 36 views
0

我想創建一個tableview只顯示5個單元格加上一個「加載更多」單元格時按下更多的單元格將被呈現。我正在下載JSON數據並將其分成3個數組,一個用於總數據,一個用於僅前5個單元的數據,另一個用於從總數據中減去前5個單元的數據後的剩餘數據。如何添加一個「加載更多」單元格,當按下時向tableview添加更多行?

我在這裏看到這個問題:Load More Cells並試圖實現他們所做的,但很少成功。這是我們至今 -

Storyboard

非常簡單,只有2個不同細胞的簡單故事板,第一個是包含JSON數據的「測試單元」,第二個是「擴大電池「當按下時應該顯示剩餘的單元格。這是到目前爲止我的代碼,我省略了很多的是要保持這個職位短,我目前得到它標誌着一個錯誤:

var castArray: [CastData?]? = [] 
    var first5CastArray: [CastData?]? = [] 
    var remainderCastArray: [CastData?]? = [] 

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

    if let firstArray = first5CastArray { 
     return firstArray.count + 1 
    } else { 
     return 0 
    } 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.row < (first5CastArray?.count)! { 
     let testCell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestCell 

     testCell.castImageView.image = first5CastImageArray?[indexPath.row] 
     return testCell 

    } else { 
     let expandCell = tableView.dequeueReusableCell(withIdentifier: "expandCell", for: indexPath) as! ExpandCell 
     expandCell.moreLabel.text = "More"//Error here -fatal error: unexpectedly found nil while unwrapping an Optional value 
     return expandCell 
    } 
    } 

//Below code never tested because of fatal error from cellForRowAt indexPath!!!!! 
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    if let remainderArray = remainderCastArray { 

     for info in remainderArray { 
     first5CastArray?.append(info) 
     } 
    } 
expandCell.isHidden = true 
     tableview.reload 
     } 

我不能完全肯定,如果我要對這個正確的方式,任何幫助表示讚賞!

+0

您確定您的moreLabel插座從您的storyBoard正確鏈接到您的expandCell類嗎? –

+0

是的,我只是檢查 – SwiftyJD

+0

檢查你的單元的標識符@SwiftyJD –

回答

0

回到故事板,再次選擇該單元格,轉至「屬性檢查器」選項卡,在identifier字段中輸入expandCell