TableViewに関して質問があります。
勉強会やカンファレンス等のイベント情報を閲覧できるアプリをご想像下さい。
イベント一覧をTableViewに表示していますが、そのviewのまま、
UISwitchのON/OFFで、一覧の中より参加済イベントのみ表示する/全て表示する と
切り替えられるように変更したいです。
参加済みか否かは「dict["PointPaid"]」に数値が入っているか否かで判断しています。
cell.isHiddenで非表示にすることはできたのですが、
非表示にした箇所が空白で空いており不格好です。
非表示かつ、高さを0にして詰めて表示したいです。
heightForRowAt内でできそうな気がするのですが、記述方法が探せませんでした、、、
アドバイスいただける幸いです。
var onlyPaid:Bool = false //スイッチ判断 var items = [NSDictionary]() //イベント情報辞書 @IBOutlet var `switch`: UISwitch! //スイッチ判断 @IBOutlet var tableView: UITableView! @IBAction func onSwitch(_ sender: UISwitch) { //スイッチ切り替え、テーブル再読込 onlyPaid = sender.isOn tableView.reloadData() } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { displayHeight = self.view.frame.height return displayHeight / 9 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "VotedContentTableViewCell") as! VotedContentTableViewCell let dict = items[(indexPath as NSIndexPath).row] let resultPoint = dict["PointPaid"] as? Int let resultButton = cell.viewWithTag(1) as! UIButton if onlyPaid == false{ //全て表示 if resultPoint != nil{ resultButton.backgroundColor = .purple }else{ resultButton.backgroundColor = .gray } } else { //参加済みのみ表示 if resultPoint == nil{ cell.isHidden = true //参加済みでない為、isHidden = true }else{ resultButton.backgroundColor = .purple //参加済み } return cell } override func viewDidLoad() { self.tableView.register(UINib(nibName: "VotedContentTableViewCell", bundle: nil), forCellReuseIdentifier: "VotedContentTableViewCell") //tableview tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .none //firebase if let userID = userid{ firebaseFunction.observeDataByNSDict( FirebaseURL:"https://firebaseio.com/User/(userID)/ParticipationContent"){ items in self.items = items self.tableView.reloadData() } } super.viewDidLoad() }
<追記>高さを条件分岐
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if onlyPaid == false{ return displayHeight / 9 } else { let dict = items[(indexPath as NSIndexPath).row] if dict["PointPaid"] as? Int == nil{ return 0.01 } else{ return displayHeight / 9 } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/17 08:31
2018/11/18 07:21 編集
2018/11/20 08:12
2018/11/20 10:25