2017-02-27 3 views
0

私はテーブルビューを作成しようとしていますが、最初に5つのセルと、さらに多くのセルが表示されます。私はJSONデータをダウンロードし、3つの配列に分割しています。合計3つの配列に分かれています。最初の5つのセルのデータと、最初の5つのセルのデータを合計データから差し引いた残りのデータの合計です。"Load More"セルを追加するには、押したときにテーブルビューにさらに多くの行が追加されますか?

私はここでこの質問を見ました:Load More Cellsと彼らは何をしたが、少しの成功と実装を試みた。これは、我々がこれまで持っているものである - 2個の異なる細胞と

Storyboard

かなり簡単、簡単なストーリーボードを、最初のものは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

ストーリーボードに戻って、そのセルを再度選択して、「属性インスペクタ」タブに移動し、expandCellidentifierフィールドに入力します。

関連する問題