2016-06-29 1 views
4

私はuitableviewcontrollerを構築しようとしており、swiftを扱うのが難しいです。numberOfRowsInSectionが呼び出されるたびに、それは4回呼び出されてからアプリケーションがクラッシュします。誰もこれを実装する方法を知っていますか?numberOfRowsInSectionがスウィフトでクラッシュする3

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

} 

テーブルには6つのアイテムがあります。確認するために配列数を表示しました。これはフルビューコントローラです。

class PCRInpatientsViewController: UITableViewController 
{ 
var listInpatient = [PCRPatient](); 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated); 


    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none; 

    self.title = "Inpatients"; 

    let view = self.view.frame; 
    let background = UIView(frame: view); 
    background.backgroundColor = Constants.medisasDarkGrey; 
    self.tableView.backgroundView = background; 

    self.definesPresentationContext = true; 

    getPatients(); 
    createUI(); 

} 

func getPatients() { 

    var array = [PCRPatient](); 
      let i = Patients.sharedInstance.patients 

      for int in 0..<i.count { 
       let d = i[int]; 

       if d.status == PCRPatientStatus.PreAdmit { 
        print(d.name) 
        array.append(d); 
       } 


      } 
    print(array.count); 
    self.listInpatient = array; 


} 

override func viewDidDisappear(_ animated: Bool) { 
    super.viewDidDisappear(animated); 

} 

func createUI(){ 


    self.navigationController?.navigationBar.barTintColor = Constants.medisasRed; 
    self.navigationController?.navigationBar.isTranslucent = false; 
    self.navigationController?.navigationBar.tintColor = UIColor.white(); 
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white()]; 


} 



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

} 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 150; 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> PCRCustomCell { 

    let patient = listInpatient[indexPath.row]; 
    print("sjddsodkso \(patient.name)"); 

    let cell = PCRCustomCell(reuse: "Inpatient", patient: patient); 
    cell.contentView.backgroundColor = Constants.medisasGrey; 


    return cell; 
} 

} 
+0

私はチェックし、配列内に6つの項目があります。私はrtheフルビューコントローラを追加しました – PCR

+0

それは元々何だったのですが、クラッシュしていたので6に変更しました。私が6と書いたらクラッシュするかどうか確認したいと思っていました。これは古いスウィフトでうまくいっていたでしょう。それがスウィフト3からのいくつかの変更が原因だと思います。 – PCR

+0

私はもっと知識のある人に延期すると思います。私は問題が何かを見ることを楽しみにしています。 –

答えて

2

Swift 3では、組み込む必要がある構文がいくつか変更されています。 Xcode 8ベータ版では、しばしばコードに正しい修正が提案されますが、必ずしもそうとは限りません。 Xcode 8のベータ版では、Shift + Command + 0と更新されたドキュメントをチェックして、ほとんど何かの最初の数文字を検索することができます。この場合、UITableViewDataSourceを検索すると、私が注意しなければならないことがすべて表示されます。

1)cellForRowAtIndexPathは現在cellForRowAtです。 NSIndexPathは今やIndexPathです。このように実装します。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // if you use prototype cells 
    let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeCellIdentifier") as! CustomUITableViewCell 

    // configure your cell 

    return cell 
} 

2)heightForRowAtIndexPathも同様に今heightForRowAtです。他の人が指摘したように、numberOfRowsInSectionはダイナミックでなければなりませんが、私はそれがクラッシュを引き起こしているとは考えていない)

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return float 
} 

3:私は個人的にこれを実装していないが、しかし、ドキュメントから、それはこのように実装です。

関連する問題