2017-11-19 4 views
-1

ビューコントローラの下部にテーブルビューがあります。どのボタンがクリックされたかに応じて、テーブルビュー上に別のものを表示しようとしています。私は自分のデータセットアップのための私の異なるプロトタイプのセルと設定方法を持っていますが、私は最後のビットを得ることができません。ここに私のコードです:クリックしたボタンに応じてtableViewに異なるデータを表示します

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var count = Int() 
    if podsButton.isSelected { 
     count = pods.count 
    } else if subscribedButton.isSelected { 
     count = subscribers.count 
    } else if subscribersButton.isSelected { 
     count = subscribed.count 
    } 
    return count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    if podsButton.isSelected{ 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as? ProfileTableViewCell else { 
      return UITableViewCell() } 
     let pod = pods[indexPath.row] 
     cell.configureCell(pod: pod) 
    } else if subscribedButton.isSelected { 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as? FollowingTableViewCell else { 
      return UITableViewCell() } 
     let user = subscribed[indexPath.row] 
     cell.configureCell(user: user) 
    } 
    return cell 
} 

今、テーブルビューは何も表示されません。 cellForRowで返すセルは、変数だけであり、デキューされた条件付きオプションの1つではありません。これは私の問題です。また、最初のオプション(ProfileCell)をデフォルトにする必要があります。何か助けてくれてありがとう。

UPDATE:今私の列挙型自分のコードを使用すると、このようになります

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var count = 0 
    if case .pods = displayState { 
     count = pods.count 
    } else if case .subscribed = displayState{ 
     count = subscribers.count 
    } else if case .subscribers = displayState { 
     count = subscribed.count 
    } 
    return count 
} 

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

    if case .pods = displayState{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as! ProfileTableViewCell 
     let pod = pods[indexPath.row] 
     cell.configureCell(pod: pod) 
     return cell 
    } else if case .subscribed = displayState { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as! FollowingTableViewCell 
     let user = subscribed[indexPath.row] 
     cell.configureCell(user: user) 
     return cell 
    } else if case .subscribers = displayState { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as! FollowingTableViewCell 
     let user = subscribers[indexPath.row] 
     cell.configureCell(user: user) 
     return cell 
    } else { 
     return UITableViewCell() 
    } 
} 

私はそうのようなボタン操作でdisplayStatesを設定しています:

@IBAction func podsButtonClicked(_ sender: BottomBorderButton) { 
    displayState = .pods 
    podsButton.addBottomBorderWithColor(color: greenColor(), width: 3) 
    subscribedButton.removeBottomBorder() 
    subscribersButton.removeBottomBorder() 
    self.tableview.reloadData() 
} 

私はこれがあると思います今はParseクエリの問題が発生しています:

// Query for the users that the user is subscribed to 
    let subscribedQuery = Following.query() 
    subscribedQuery?.whereKey("follower", equalTo: PFUser.current()?.objectId as Any) 
    subscribedQuery?.findObjectsInBackground(block: { (objects, error) in 
     if let objects = objects { 
      for object in objects { 
       self.subscribed.insert(object as! PFUser, at: 0) 
      } 
     } 
    }) 

エラー:

Could not cast value of type 'Speakable.Following' (0x100552ec0) to 'PFUser' (0x100a12808).

UPDATE ***は

includeKeyでこれを修正が、今キャッチされない例外により 'NSInternalInconsistencyException'、理由にアプリを終了

***

を取得しています:「このクエリには未解決のネットワーク接続があります。あなたはそれが完了するまで待たなければなりません。

+0

は、あなたのボタンアクションで 'reloadData'を呼び出していますか? – rmaddy

+0

@rmaddy私はそれを試してみませんか?今はボタンの下線を追加していました。デキューされたセルではなく、最初のセル "let cell = UITableVIewCell" – SalokinSwift

+1

'cellForRowAt'から最初の' let cell = '行を削除する必要があります。また、 'guard'キーワードを削除し、結果を強制的にキャストする必要があります。そして各ブロックの中に 'return cell'を追加します。 – rmaddy

答えて

0

コードを変更する必要があります。

viewDidLoadの初期ボタンを必ず選択してください。あなたのボタン操作でreloadDataに電話してください。

cellForRowAtを適切にリファクタリングする必要があります。あなたの現在のコードでは、guardcell変数は、最初のifの前の変数cellとは異なりますので、無駄な空のUITableViewCellインスタンスを返すことがあります。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if podsButton.isSelected{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as! ProfileTableViewCell 
     let pod = pods[indexPath.row] 
     cell.configureCell(pod: pod) 

     return cell 
    } else if subscribedButton.isSelected { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as! FollowingTableViewCell 
     let user = subscribed[indexPath.row] 
     cell.configureCell(user: user) 
     return cell 
    } else if subscribersButton.isSelected { 
     // similar code to create and return cell 
    } else { 
     fatalError("Unexpected - no button selected") // Fix your code 
     return UITableViewCell() 
    } 
} 

これはフォースキャスティングを適切に使用するのに適しています。 guardステートメントの必要はありません。あなたのセルが正しく設定されていない場合、開発中にこのコードをクラッシュさせたい。正しく設定した後は、実行時に間違っていることはありません。また

、あなたのnumberOfRowsInSectionの最初の行は次のようになります。

var count = 0 
+0

素晴らしい応答に感謝します。まだデータを表示していない、別のオプション "else"が空のセルを表示していると思います。 .isSelectedはここで使用する適切な用語ですか?ボタンごとにボタンアクション、つまりpodsButtonIsClickedがあります。代わりにこの行動を呼びますか? (送信者:) – SalokinSwift

+1

個人的には、私はテーブルビューの3つの可能な状態を定義する 'enum'プロパティを持っています。その後、すべてのデータソース/ボタンの状態をチェックするのではなく、enum値を使用するメソッドを委譲し、ボタンのアクションを更新して、それに応じてenumプロパティを更新します(テーブルビューもリロードします)。 – rmaddy

+1

ちょうど列挙型を使用することを提案しようとしていました。ボタンや他のビューの状態を使ってプログラムのロジックを制御するためには、テーブルビューを生成するために使用するセルとソース配列の型を制御するenumインスタンス変数を用意し、そのインスタンス変数をボタンアクションから設定する必要があります。 。 –

関連する問題