2017-01-28 1 views
0

私はSwift 3.0でプロトタイプのアプリケーションをまとめています。Swift 3/Xcode 8で "active"カラムが "no"の場合にセルを非表示にする方法

分ですべてのAPI情報がテーブルセルに解析されます。私が望んでいたのは、データベースの「アクティブな」列を「いいえ」に設定すると、そのセルがテーブルビューに表示されないようにする方法を紹介することでした。

現在のコードはこれです:

func configure(offence: Offence) { 
    if let name = offence.name, let act = offence.act { 
     self.textLabel?.text = name 
     self.detailTextLabel?.text = act 
    } 

私がもし/ else文で遊んでみましたが、私は多くの幸運を持っていませんよ。あなたのOffence sは、あなたのサーバの応答をフィルタリングし、それを使用することができますactive性質を持っている場合

+0

さらにコードを表示できますか?あなたはテーブルビューのデータソースとして使用する 'Offence'オブジェクトの配列を持っていますか? –

答えて

1

のみtrueに設定active性質を有するものを示しています。

struct Offence { 
    var name: String 
    var active: Bool 
} 

class YourViewController: UITableViewController { 
    // all the offences you get from the server 
    let offences: [Offence] = [] 

    // only the active offences - use those as your datasource! 
    var visibleOffences: [Offence] { 
     return offences.filter { $0.active } 
    } 

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "YourCell", for: indexPath) 
     cell.textLabel?.text = visibleOffences[indexPath.row].name 
     return cell 
    } 

} 
+0

'' offences''オブザーバーを '' visibleOffences''を1回だけ設定して 'visibleOffences' – muescha

+0

を呼び出すたびにフィルタリングするのを避けるか、YourViewControllerのログインをそのままにしておくと良いでしょう。犯行: 'myViewController.offences = myOriginalOffencesFromDatabase.filter {$ 0active}' – muescha

+0

@muescha:他の方法、ええ! –

関連する問題