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

标签 ios swift xcode uitableview memory

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

我想做的是允许它从另一个 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/

相关文章:

ios - Swift 4.2 委托(delegate)和协议(protocol)不起作用

iphone - 围绕屏幕外的一个点旋转 UIImageView? (uiImageView 的中心

swift - 在不创建新常量的情况下将可选值解包到保护条件中的预定义变量

iOS 无法删除通知观察器。 Deinit 没有被调用

iphone - 进程的生命周期 ios

iOS 重定向到设置主页

ios - 横向模式下 UITextField 中间出现奇怪的黑条

ios - 如何在表格 View 单元格上应用复选标记

swift - 移动标签而不将其嵌套在屏幕填充 UIView 下

xcode - Storyboard内部不一致