2017-03-20 13 views
0

私はテーブルビューを表示したいが、各アイテムの "ステータス"によってセクションごとに区切っているという問題があります。私は、単純な文字列配列でそれを行う方法を知っているが、私はクラス(Aluno)配列で、この作業をするために得ることができない、ここに私のコードは、これまでのところです:UITableViewがすべてのセクションを表示していません

import UIKit 

class DeliveriesTVC: UITableViewController { 

    let sections = ["Delivered", "Not Delivered"] 
    var studentList: [Array<Student>] = [[], []] 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     for i in 0...5{ 
      studentList[0].append(Student(alunoNome: "Aluno \(i)", alunoImg: "dani_test", alunoStatus: "Delivered")) 
     } 

     for i in 6...10{ 
      studentList[1].append(Student(alunoNome: "Aluno \(i)", alunoImg: "dani_test", alunoStatus: "Not Delivered")) 

     } 

     self.title = "Deliveries" 
    } 

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

     if let cellEntrega = tableView.dequeueReusableCell(withIdentifier: "EntregaCell", for: indexPath) as? EntregaCell { 

      let entregaCell = studentList[indexPath.section][indexPath.row] 

      // here i call a function in my TableViewCell class that update the cell itself 
      cellEntrega.updateAlunoUI(Aluno: entregaCell) 

      return cellEntrega 

     } else { 
      return UITableViewCell() 
     } 
    } 


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return listaAlunos[section].count 
} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return self.sections[section] 
} 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 2 
    } 
} 

出力では、私はちょうど取得します「第1セクション」は名前を付けずに、各セクションの名前とセクションの数を設定しても表示されます。私はどこにでも見ましたが、私は解決策を見つけることができませんでした。あなたのnumberOfSectionInTableView機能については

+0

実際には、データソースを把握する必要があります。あなたの 'cellForRowAt'は' studentList'という名前の配列の配列に基づいています。あなたの 'numberOfRowsInSection'は' listaAlunos'という名前の配列に基づいています(おそらく翻訳の見落とし)。 'titleForHeaderInSection'は' sections'という別の配列に基づいています。そして、 'numberOfSections'を' 2'の値で不必要にハードコードします。その数字を厳しくしないでください。 – rmaddy

+0

ええ、それは翻訳問題です、彼らは実際には同じ、申し訳ありません!私は文字通りすべてを試したのでセクションの数をハードコーディングしました。そして最終的に私の問題は間違っていて分かりませんでした。助けてくれてありがとう! –

答えて

0

をごnumberOfSectionstitleForHeader方法が間違っている、それはまた

override func numberOfSections(in tableView: UITableView) -> Int { 

    return 2 
} 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    return self.sections[section] 
} 

する必要があり、あなたがすべきreturn 2の代わりにself.sections.countnumberOfSectionsに戻すと、別のオブジェクトを配列に追加する場合と同じようにハードコードされますので、配列を変更する必要があります。

+0

ありがとう非常に!私のために働いた! –

0
  1. 、それはtableView: UITableViewの前でin
    override func numberOfSectionsInTableView(in tableView: UITableView) -> Int { return 2 }
    すべきではありませんか?

  2. UITableViewdelegatedatasourceをどこに接続するのかわかりません。

Hereは、UITableViewの使い方を示すチュートリアルです。

「基本表ビュー」を見てみましょう - 「ステップ3:データソースとデリゲートを設定する」を

+0

あなたは 'UITableViewController'にデリゲートとデータソースを設定する必要はありません – Rikh

+0

あなたは正しいです。何とか私はそれが 'UIViewController'にあると思った。 –

関連する問題