gpt4 book ai didi

ios - IOS 开发新手,点击评论单元格 Thread 1 : signal SIGABRT and terminating app due to uncaught exception 时崩溃

转载 作者:行者123 更新时间:2023-11-28 07:28:13 25 4
gpt4 key购买 nike

应用程序构建成功,但在运行应用程序后,在我单击评论单元格后,应用程序崩溃,导致错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 0]' and Thread 1: signal SIGABRT.

在下图中,每当我点击带有随机评论的任何 Alec1 时,应用程序都会崩溃。

https://i.ibb.co/Sn5gFZ3/Screen-Shot-2019-04-22-at-12-49-19-AM.png

var posts = [PFObject]()    
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self

// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)


let query = PFQuery(className: "Posts")
query.includeKeys(["author", "comments", "comments.author"])
query.limit = 20
query.findObjectsInBackground{ (posts, error) in
if posts != nil{
self.posts = posts!
self.tableView.reloadData()
}
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let post = posts[section]
let comments = (post["comments"] as? [PFObject]) ?? []
return comments.count + 1
}

func numberOfSections(in tableView: UITableView) -> Int {
return posts.count
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let post = posts[indexPath.section]
let comments = (post["comments"] as? [PFObject]) ?? []


if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! PostCell


let user = post["author"] as! PFUser
cell.usernameLabel.text = user.username

cell.captionLabel.text = post["caption"] as! String


let imageFile = post["image"] as! PFFileObject
let urlString = imageFile.url
let url = URL(string: urlString!)!

cell.photoView.af_setImage(withURL: url)

return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell") as! CommentCell

let comment = comments[indexPath.row - 1]
cell.commentLabel.text = comment["text"] as? String
let user = comment ["author"] as! PFUser
cell.nameLabel.text = user.username
return cell


}
}
//creates new columns
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.row]
let comment = PFObject(className: "Comments")
comment["text"] = "random comment"
comment["post"] = "post"
comment["author"] = PFUser.current()
post.add(comment, forKey: "comments")
post.saveInBackground{(success, error) in
if success{
print("comment saved!")

}else{
print("error saving comments")
}

}

}

预期的结果是点击评论单元格的某些部分后,应用程序不应崩溃

最佳答案

didSelectRowAt中使用section获取post然后选择评论,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.section]
let comments = (post["comments"] as? [PFObject]) ?? []

let comment = comments[indexPath.row]
comment["text"] = "random comment"
comment["post"] = "post"
comment["author"] = PFUser.current()
post.add(comment, forKey: "comments")
post.saveInBackground{(success, error) in
if success{
print("comment saved!")

}else{
print("error saving comments")
}

}

}

关于ios - IOS 开发新手,点击评论单元格 Thread 1 : signal SIGABRT and terminating app due to uncaught exception 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55791101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com