Ragini
Ragini

Reputation: 332

How to delete section of UITableView on click of view in swift3

I want to remove a table cell on click of view which is on cell in swift3.

Following function is called on click of View and remove method is used to delete table section.But this solution is not working.

func remove(sender: UITapGestureRecognizer)
        {
            let indexView = sender.view
            let index = indexView?.tag

            self.removeAddresses(position: index!)
        }

    func remove(position: Int)  
    {  
        tableView.deleteSections(IndexSet(index: position,with. automatic))  
        tableView.reloadData()
    }

Upvotes: 0

Views: 1932

Answers (2)

dengApro
dengApro

Reputation: 4008

  1. To remove a table cell, I suggest you can use Editing Style

Methods:

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

            }   
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

}        
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

        }   
  1. And I think ,

      func remove(sender: UITapGestureRecognizer){
                let indexView = sender.view
                let index = indexView?.tag       
                self.removeAddresses(position: index!)         
            }
    
      func removeAddresses(position: Int){  
         tableView.beginUpdates()
         yourDataSourceArray.removeObject(at: position)
         table.deleteRows(at: [indexPathCreated] , with: .automatic)
         table.endUpdates()
       }
    

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

Try this :

func remove(position: Int)  
    {  
        yourDataSourceArray?.removeAtIndex(position)

        tableView.deleteSections(IndexSet(index: position,with. automatic))  
        tableView.reloadData()
    }

Upvotes: 1

Related Questions