当使用段控件填充不同段下的相应列表时,我面临的问题如下
当在 TableView 中加载段控件时,第一个段工作正常,当我切换第二个段时,它确实会填充列表,但在以下情况下会给出索引超出范围的错误列表滚动
第二个段控件应根据过滤器加载不同的列表,但它加载与第一个段控件相同的列表
我为此使用 Swift IOS 和 Firestore 数据库 下面是我分享的代码
class FirstSegementViewController: UIViewController {
@IBOutlet var segmentControl:UISegmentedControl!
@IBOutlet var tableView: UITableView!
var firstDetails:[FirstDetails] = []
var secondDetails:[SecondDetails] = []
var postKey:String = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view.
retrieveAllPosts()
}
func retrieveAllPosts(){
let postsRef = Firestore.firestore().collection("posts").whereField("post_author_id", isEqualTo: Auth.auth().currentUser!.uid
).whereField("status", isEqualTo: false).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 = FirstDetails(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)
self.firstDetails.append(newSourse)
// print(self.postKey)
}
self.tableView.reloadData()
}
}
}
}
func retrieveAllPosts2(){
let postsRef = Firestore.firestore().collection("posts").whereField("post_author_id", isEqualTo: Auth.auth().currentUser!.uid).whereField("status", isEqualTo: true).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 newSourse1 = SecondDetails(_documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent, _postuserprofileImagUrl: postAuthorProfilePicUrl, _postAuthorSpinncerC: postAuthorSpinnerC)
self.secondDetails.append(newSourse1)
}
self.tableView.reloadData()
}
}
}
}
@IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
retrieveAllPosts()
// label1.text = "First Segment Selected"
case 1:
retrieveAllPosts2()
// label1.text = "Second Segment Selected"
default:
break
}
//self.tableView.reloadData()
}
@objc func toComments(_ sender: AnyObject) {
let commentbutton = sender as! UIButton
let post = firstDetails[commentbutton.tag]
postKey = post._documentId // or what key value it is
print("hello")
performSegue(withIdentifier: "toCommentsListforMWs", sender: self)
}
// 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.
var vc = segue.destination as! CommentListViewController
vc.postId = postKey
}
/*
// 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 = firstDetails.count
break
case 1:
value = secondDetails.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.firstdetails1 = firstDetails[indexPath.row]
cell.commentbutton.tag = indexPath.row
cell.commentbutton.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
break
case 1:
cell.completed1 = secondDetails[indexPath.row]
cell.commentbutton.tag = indexPath.row
cell.commentbutton.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
break
default:
break
}
return cell
}
}
最佳答案
更改您的 indexChanged(_ sender: UISegmentedControl)
函数,如下所示:
@IBAction func indexChanged(_ sender: UISegmentedControl) {
switch segmentControl.selectedSegmentIndex
{
case 0:
self.firstDetails.removeAll()
retrieveAllPosts()
// label1.text = "First Segment Selected"
case 1:
self.secondDetails.removeAll()
retrieveAllPosts2()
// label1.text = "Second Segment Selected"
default:
break
}
//self.tableView.reloadData()
}
关于ios - 错误 : Index out of range in segment control,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987062/