ビューコントローラの下部にテーブルビューがあります。どのボタンがクリックされたかに応じて、テーブルビュー上に別のものを表示しようとしています。私は自分のデータセットアップのための私の異なるプロトタイプのセルと設定方法を持っていますが、私は最後のビットを得ることができません。ここに私のコードです:クリックしたボタンに応じて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'、理由にアプリを終了
***
を取得しています:「このクエリには未解決のネットワーク接続があります。あなたはそれが完了するまで待たなければなりません。
は、あなたのボタンアクションで 'reloadData'を呼び出していますか? – rmaddy
@rmaddy私はそれを試してみませんか?今はボタンの下線を追加していました。デキューされたセルではなく、最初のセル "let cell = UITableVIewCell" – SalokinSwift
'cellForRowAt'から最初の' let cell = '行を削除する必要があります。また、 'guard'キーワードを削除し、結果を強制的にキャストする必要があります。そして各ブロックの中に 'return cell'を追加します。 – rmaddy