从部分中删除行

发布于 2025-01-15 00:54:45 字数 1373 浏览 5 评论 0原文

这是我的sectionArray,

 var sectionArray = [Sections]()

这是我计算部分中有多少行的方法

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sectionArray[section].items.count
}

,这是我通过滑动删除行的代码。

  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCell.EditingStyle.delete {
        sectionArray[indexPath.section].remove(at: indexPath.row)
        if sectionArray[indexPath.section].title.count == 0 {
            sectionArray.remove(at: indexPath.row)
        }
        tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
    }
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
    let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
        //self.isEditing = false
        print("delete button tapped")
    }
    delete.backgroundColor = UIColor.red
    return [delete]
}

理论上,这个删除的东西应该可以工作,但我收到错误没有精确匹配的下标调用

sectionArray[indexPath.section].remove(at: indexPath.row)

This is my sectionArray

 var sectionArray = [Sections]()

This is how I figure out how many rows in section

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sectionArray[section].items.count
}

and this is my code to remove a row by sliding.

  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCell.EditingStyle.delete {
        sectionArray[indexPath.section].remove(at: indexPath.row)
        if sectionArray[indexPath.section].title.count == 0 {
            sectionArray.remove(at: indexPath.row)
        }
        tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
    }
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
    let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
        //self.isEditing = false
        print("delete button tapped")
    }
    delete.backgroundColor = UIColor.red
    return [delete]
}

Theoretically this delete stuff should work but I am getting error No exact matches in call to subscript at

sectionArray[indexPath.section].remove(at: indexPath.row)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2025-01-22 00:54:45
/// This line
sectionArray.remove(at: indexPath.row)
/// should be like this
sectionArray.remove(at: indexPath.section)
/// This line will remove the section from the table 
tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)

您正在使用行删除一个部分

编辑

替换您向我们提供的所有代码

      /*
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete {
            sectionArray[indexPath.section].remove(at: indexPath.row)
            if sectionArray[indexPath.section].title.count == 0 {
                sectionArray.remove(at: indexPath.row)
            }
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        }
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            //self.isEditing = false
            print("delete button tapped")
        }
        delete.backgroundColor = UIColor.red
        return [delete]
    }
     */
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            //self.isEditing = false
            sectionArray[indexPath.section].remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
            if sectionArray[indexPath.section].items.isEmpty {
                sectionArray.remove(at: indexPath.section)
                tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
            }
        }
        delete.backgroundColor = UIColor.red
        return [delete]
    }
    
/// This line
sectionArray.remove(at: indexPath.row)
/// should be like this
sectionArray.remove(at: indexPath.section)
/// This line will remove the section from the table 
tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)

You are using row to remove a section

Edit

Replace all the code you provided us with this

      /*
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete {
            sectionArray[indexPath.section].remove(at: indexPath.row)
            if sectionArray[indexPath.section].title.count == 0 {
                sectionArray.remove(at: indexPath.row)
            }
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        }
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            //self.isEditing = false
            print("delete button tapped")
        }
        delete.backgroundColor = UIColor.red
        return [delete]
    }
     */
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            //self.isEditing = false
            sectionArray[indexPath.section].remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
            if sectionArray[indexPath.section].items.isEmpty {
                sectionArray.remove(at: indexPath.section)
                tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
            }
        }
        delete.backgroundColor = UIColor.red
        return [delete]
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文