开发者社区> 问答> 正文

swift无法从Firestore数据库加载分段控件TableView中的数据?

0

我正在尝试从Firestore数据库中填充段控制tableview,但是由于某种原因而无法这样做,我正在尝试一个在线示例,该示例具有预设数据,但是在这里我正在从Firestore数据库中检索数据。

它也不会给出任何运行时错误,只是不加载数据。下面是viewcontroller的屏幕截图作为代码。 class FirstSegementViewController: UIViewController {

@IBOutlet var segmentControl:UISegmentedControl!

@IBOutlet var tableView: UITableView!

var s1Post:[s1] = []
var s2Post:[s2] = []


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    retrieveAllPosts()
}


func retrieveAllPosts(){
    let postsRef = Firestore.firestore().collection("posts").limit(to: 50)

    postsRef.getDocuments { (snapshot, error) in

        if let error = error {

            print(error.localizedDescription)

        } else {

            if let snapshot = snapshot {

                for document in snapshot.documents {


                    let data = document.data()
                    //self.postKey = document.documentID
                    let username = data["post_author_username"] as? String ?? ""
                    let postTitle = data["postTitle"] as? String ?? ""
                    let postcategory = data["postcategory"] as? String ?? ""
                    let postContent = data["postContent"] as? String ?? ""
                    let postAuthorProfilePicUrl = data["post_user_profile_pic_url"] as? String ?? ""
                    let postAuthorSpinnerC = data["post_author_spinnerC"] as? String


                    let newSourse = s1(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)

                    self.s1Post.append(newSourse)
                    // print(self.postKey)
                }
                self.tableView.reloadData()
            }
        }
    }
}




@IBAction func indexChanged(_ sender: UISegmentedControl) {
      switch segmentControl.selectedSegmentIndex
     {
     case 0:
        retrieveAllPosts()
     // label1.text = "First Segment Selected"
     case 1:

        retrieveAllPosts()
     // label1.text = "Second Segment Selected"
     default:
     break
     }

    //self.tableView.reloadData()
}

/*
 // MARK: - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 // Get the new view controller using segue.destination.
 // Pass the selected object to the new view controller.
 }
 */



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

}

extension FirstSegementViewController: UITableViewDelegate, UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var value = 0
    switch segmentControl.selectedSegmentIndex{
    case 0:
        value = s1Post.count
        break
    case 1:
        value = s2Post.count
        break
    default:
        break
    }
    return value
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyWsPostCell
    switch segmentControl.selectedSegmentIndex{
    case 0:
        cell.s11 = s1Post[indexPath.row]
        break
    case 1:
        cell.s21 = s2Post[indexPath.row]
        break

    default:
        break
    }
    return cell
}

}

展开
收起
被纵养的懒猫 2019-09-25 15:17:26 481 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载