Search code examples
iosswiftuitableviewaccessorytype

Swift How to reset my Tableview Cells accessoryType to .None from a button


I'm a beginner in iOS development and I'm using swift.

I'm Trying to program a simple to do list app. where you can checkmark items after completing and click on done button to erase them all at once.

The Button was successfully deleting the selected items after storing them in an array and then removing from the main array

"Done" button code is bellow

    @IBAction func btnDone(sender: AnyObject) {
    for i in selectedItems {
        items.removeAtIndex(i)
    }
    tableview.reloadData()
}

but the problem is that the check marked does not go (or reseted to .None)

i found a solution which is easy for me to implement on the following link: How to reset the tableview cells accessorytype to none on a click of a button?

but unfortunately is in Objective-C, Can somebody translate to Swift or provide similar solutions


Solution

  • Please, try this..

            @IBAction func btnDone(sender: AnyObject) {
                for (var section = 0, sectionCount = tableView.numberOfSections; section < sectionCount; ++section) {
                    for (var row = 0, rowCount = tableView.numberOfRowsInSection(section); row < rowCount; ++row) {
                        var cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))
                        cell!.accessoryType = UITableViewCellAccessoryType.None
                        cell!.selectionStyle = UITableViewCellSelectionStyle.None
                    }
                }
            }