juelizabeth
juelizabeth

Reputation: 505

delete tableView cells

I have a table view controller and I have embedded a left bar edit item button.

 override func viewDidLoad() {
        super.viewDidLoad()
navigationItem.leftBarButtonItem = self.editButtonItem
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if (editingStyle == UITableViewCellEditingStyle.delete) {

            MessageTableView.deleteRows(at: [indexPath], with: .fade)
        }

}

When I click the edit button, a circle pops up in order to place a check mark in it enter image description here

But when I click the edit button I want the red minus sign to appearenter image description here

But every tutorial I follow, I get the first picture and I don't want that.

Upvotes: 0

Views: 120

Answers (2)

Amul4608
Amul4608

Reputation: 1448

var arrayDays:[Student] = []

    @IBOutlet var tableViewForStudents: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let std1 = Student(name: "First", rollNo: 1, address: "this is \\n address of \\n student 1",image:"demo.png")
        let std2 = Student(name: "Two", rollNo: 2, address: "this is \\n address of \\n student 2",image:"demo.png")
        let std3 = Student(name: "Three", rollNo: 3, address: "this is \\n address of \\n student 3",image:"demo.png")
        let std4 = Student(name: "Four", rollNo: 4, address: "this is \\n address of \\n student 4",image:"demo.png")
        let std5 = Student(name: "Five", rollNo: 5, address: "this is \\n address of \\n student 5",image:"demo.png")
        let std6 = Student(name: "Six", rollNo: 6, address: "this is \\n address of \\n student 6",image:"demo.png")

        arrayDays.append(std1)
        arrayDays.append(std2)
        arrayDays.append(std3)
        arrayDays.append(std4)
        arrayDays.append(std5)
        arrayDays.append(std6)

    }
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {
            arrayDays.remove(at: indexPath.row)

            tableView.deleteRows(at: [indexPath], with: .left)
        }

import UIKit

class Student: NSObject {

    var strStudentName:String!
    var strStrudentRollNo:Int!
    var strStudentAddress:String!
    var imageName:String!

    init(name:String, rollNo:Int, address:String,image:String) {

        strStudentName = name
        strStrudentRollNo = rollNo
        strStudentAddress = address
        imageName = image
    }
    }

Upvotes: 0

rmaddy
rmaddy

Reputation: 318934

Based on your comments below the question, you have enabled the allowsMultipleSelectionDuringEditing property for the table view. With this enabled, the table supports multiple selection while the table view is being edited. This takes precedence over supporting "normal" editing features such as deletion. This is why you get the checkbox (for selection) instead of the red deletion circle.

If you want to support deleting cells with the red circle while in editing mode, you need to disable multiple selection during editing.

Upvotes: 2

Related Questions