こちら↓の質問から派生しています。
リンク
やりたいこと
データ表示しているtableViewCellのIndexPath
をInt
で取得したいです。
試したこと・試していること
- macOSのtableViewメソッドにある
selectAll
のiOS版(tableViewで使用可能な)がないか探したり尋ねたりしました。
→結果:ないので自作、という結論に。
selectRow
メソッドをfor in
文内で使用し、どのような動きになるのか確認したいと思い試している最中でIndexPath
がInt
で所得できずに詰まっています。(IndexPath
(Int)ではなく[IndexPath]
で取得してしまっています。)
以下、[IndexPath]
で取得してエラーが出ているコードです。なお、tableViewCellに表示させるデータはfirebase firestoreから取得しています。(UITableViewDelegateは省略しております)
swift
import UIKit import Firebase class DraftViewController: UIViewController, UITextFieldDelegate { let db = Firestore.firestore() let user = Auth.auth().currentUser private var draftData:[QueryDocumentSnapshot] = [] override func viewDidLoad() { super.viewDidLoad() //複数選択できるようにする tableView.allowsMultipleSelectionDuringEditing = true //firestoreのdraftsデータを取得 getDraftsData() } //firestoreのdraft data を取得 func getDraftsData() { guard let uid = user?.uid else { return } db.collection("drafts").whereField("uid", isEqualTo: uid).getDocuments() { (querysnapshot, err) in if let err = err { print("error getting documents: (err)") } else { let personalDraftData = querysnapshot!.documents.sorted {personalDraftData,provisionalDraftData in guard let personalDraftsData = personalDraftData.data()["timestamp"] as? Timestamp, let provisionalDraftsData = provisionalDraftData.data()["timestamp"] as? Timestamp else { return false } return (personalDraftsData.dateValue() > provisionalDraftsData.dateValue()) } self.draftData = personalDraftData self.tableView.reloadData() } } } }
swift
extension DraftViewController: UITableViewDataSource { @IBAction func tapCloseButton(_ sender: Any) { guard let indexPaths = tableView.indexPathsForVisibleRows else { return } let allIndexPaths = indexPaths.count guard let imageView = UIImage(named: "check mark") else { return } if closeButton.image == nil && closeButton.title == "Check all" { //全てのtable cell を選択する for _ in 0 ..< allIndexPaths { tableView.selectRow(at: indexPaths, animated: true, scrollPosition: .none) } } else if closeButton.image == nil && closeButton.title == "Unselect all" { // 選択しているcellのチェックマークを全て外す(クリアにする) } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return draftData.count } //各cellの要素設定(インスタンスを生成する) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // cellを取得する guard let cell = tableView.dequeueReusableCell(withIdentifier: "draftTableCell", for: indexPath) as? CustomizedTableViewCell else { return UITableViewCell() } //cellに表示する値を設定する cell.titleLabel.text = draftData[indexPath.row].data()["title"] as? String cell.detailLabel.text = draftData[indexPath.row].data()["detail"] as? String cell.titleLabel.numberOfLines = 0 cell.detailLabel.numberOfLines = 0 cell.titleLabel.lineBreakMode = .byWordWrapping cell.titleLabel.lineBreakMode = .byWordWrapping return cell } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { } } }


まだ回答がついていません
会員登録して回答してみよう