Emi Emi - 6 months ago 116
Swift Question

How to make checklist in UITableView - Swift?

I made an app with

UITableView
. I want to show a tick mark in the left when cell is touched, and to be hidden when again is touched. I used some code and It's not showing as I wanted. The tick is showing on the right not on the left of screen.

This is how I want to make:
enter image description here

This is how it is:
enter image description here

And here is code that I used:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

override func viewDidLoad() {
super.viewDidLoad()

self.myTableView.allowsMultipleSelection = true
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None
}


So at the conclusion I would like to mention again problems.
First problem is that the tick is showing on the right of the cell and not on the left. And other problem it won't to untick (hide when cell is pressed again)

Thanks.

Answer

Try this

func getIndexPathFromCell(sender: UIButton) -> NSIndexPath! {
        if let view = sender.superview {
            var superView = view
            while (!superView.isKindOfClass(UITableViewCell.classForCoder())) {
                if let viewSuperView = superView.superview {
                    superView = viewSuperView
                } else {
                    return nil
                }
            }
            if let cell = superView as? GoalsTableViewCell {
                return myTableView.indexPathForCell(cell)
            } else {
                return nil
            }
        } else {
            return nil
        }
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "DhikrTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GoalsTableViewCell

        cell.tickButton.addTarget(self, action: #selector(GoalsViewController.toggleSelcted(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        // Fetches the appropriate meal for the data source layout.
        let workout = workouts[indexPath.row]
        let number = numbers[indexPath.row]

        if workout.isSelected {
            cell.tickButton.setImage(UIImage(named: "Ticked Button"), forState: UIControlState.Normal)
        } else {
            cell.tickButton.setImage(UIImage(named: "Tick Button"), forState: UIControlState.Normal)
        }

        cell.nameLabel.text = workout.name
        cell.numberLabel.text = number.number

        return cell
    }
func toggleSelcted(button: UIButton) {
        let indexPath = getIndexPathFromCell(button)
        let workout = workouts[indexPath.row]
        workout.isSelected = !workout.isSelected

        myTableView.reloadData()
    }