Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Remove Row From Section

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)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

/// 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]
    }
    
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda