gpt4 book ai didi

ios - 两个带有数据的 TableView ,并希望从一个特定的 TableView 中删除索引。我还必须删除另一个数组吗?

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

好的,所以我从中得到了内存错误,这很烦人,因为它不是简单的语法错误。好吧,我正在做的事情是了解如何操作数据

我想做的是允许它从另一个 View 中删除,但它仍然崩溃。我将分享我正在进行的两个 TableView 。

这是有问题的,因为它在第二个 View Controller 中

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FeedCommands.commentSection.count
}

//allows us to delete the code
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// this is called from a static variable class function
FeedCommands.RemoveComment(atIndex: indexPath.row)
CommentFeed.deleteRows(at: [indexPath], with: .fade)
}
}

现在这是一个完美的工作

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FeedCommands.feedArray.count
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
FeedCommands.feedArray.remove(at: indexPath.row)
TabView.deleteRows(at: [indexPath], with: .fade)
}
}

如果我运行问题一,它因内存错误而退出,我认为它可能必须处理数组,这可能是真正的原因。我搜索了一下,在执行多个表格 View 时,似乎没有任何内容可以涵盖这种情况。

这是我从中调用数组的类

static var commentSection: Array<String> = []
class func AddToComment(newElement: String){
FeedCommands.commentSection.append(newElement)
}
class func RemoveComment (atIndex: Int){
FeedCommands.commentSection.remove(at: atIndex)
}

static var QuestionToComment: Array<String> = []
class func AddQuestionToComment(newElement: String){
FeedCommands.QuestionToComment.append(newElement)
}
class func RemoveQuestionToComment (atIndex: Int){
FeedCommands.QuestionToComment.remove(at: atIndex)
}

static var feedArray: Array<String> = []

class func AddToFeed (newElement: String){
FeedCommands.feedArray.append(newElement)
}
class func Remove (atIndex: Int){
FeedCommands.feedArray.remove(at: atIndex)
}

如果需要更多详细信息,请告诉我。

编辑:应要求

这是评论区文件

import UIKit

class Comments: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var Question: UITextView!

@IBOutlet weak var ReplyTextField: UITextField!
@IBOutlet weak var CommentFeed: UITableView!
@IBAction func SubmitReply(_ sender: UIButton) {
CommentFeed.reloadData()
if ReplyTextField.text == nil {
}
else{
FeedCommands.AddToComment(newElement: ReplyTextField.text!)
ReplyTextField.text = ""
}
// dismiss(animated: true, completion: nil)
// ReplyTextField.placeholder = "Comment"


}
override func viewDidLoad() {
super.viewDidLoad()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//this is what we use to get the question to
//appear in the comment section
Question.text = ""
for Section in FeedCommands.QuestionToComment{
Question.text = "\(Section)"
CommentFeed.reloadData()
}

}

//adding the table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FeedCommands.commentSection.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = CommentFeed.dequeueReusableCell(withIdentifier: "Com", for: indexPath)
cell.textLabel?.text = "@\(Question.text!)__ \(FeedCommands.commentSection[indexPath.row])"
return cell
}

//allows us to delete the code
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
FeedCommands.commentSection.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade) // crashes here
}
}
}

最佳答案

您在将新元素附加到表格 View 之前重新加载表格 View 数据,而不是在附加回复后重新加载它。这就是为什么您需要按两次按钮才能使其出现。修复提交回复操作实际上应该做的是在每次按下 SubmitReply 按钮时在 TableView 的最后一个索引(计数 - 1)处插入一个新行:

@IBAction func SubmitReply(_ sender: UIButton) {
if !ReplyTextField.text!.isEmpty{
FeedCommands.AddToComment(newElement: ReplyTextField.text!)
ReplyTextField.text = ""
CommentFeed.insertRows(at: [IndexPath(row: FeedCommands.commentSection.count-1, section: 0)], with: .automatic)
}
}

关于ios - 两个带有数据的 TableView ,并希望从一个特定的 TableView 中删除索引。我还必须删除另一个数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48350817/

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