0

I have two table view controllers, the 2nd tableview controller contains entries with different option to add to the first view table view controller. The first table view controller rows have two swipe actions i.e. completed and delete.

Each row/cell has two labels one with the title of the option selected from 2nd table view controller and 2nd label is the date it was created.

When completed swipe action is tapped, strike through function strikes the text of the both the labels.

However, I am noticing that everytime I swipe an entry and delete it, then add the same entry again, it would automatically swipe the title label of the cell.

If I go back and relaunch the 1st view controller, it clears the stike.

This striking happens randomly i.e. it strikes sometimes and some times it does not strike and the strike through function is not even called.

Any suggestions are greatly appreciated.

View controller1:

import UIKit

class nTableViewController: UITableViewController {

    @IBOutlet weak var progressBar: UIProgressView!


    var nItems:[TodoItem]! {

        didSet {
                progressBar.setProgress(progress, animated: true)
        }



    }
    var progress:Float{
        if nItems.count > 0 {
            return Float(nItems.filter({$0.completed}).count) / Float(nItems.count)
        }else {
            return 0
            }
        }




    var selectedTitle = String() {
        didSet {
            creatensItem(with: selectedTitle, modalDismiss: true)
    }
    }

    @IBAction func unwindtonsTBVC(segue:UIStoryboardSegue) { }



    //
    func  creatensItem(with titleRcvd:String, modalDismiss:Bool) {

            let newnItem = TodoItem.init(title: titleRcvd, completed: false, createdAt: Date(), itemIdentifier: UUID())
            newnItem.saveItem()
            if nItems == nil  {
            self.tableView.beginUpdates()
            self.nItems.append(newnItem)
            let indexPath = IndexPath(row: self.tableView.numberOfRows(inSection: 0), section:0)
            self.tableView.insertRows(at: [indexPath], with: .automatic)
            progressBar.setProgress(progress, animated: true)
            self.tableView.endUpdates()
        } else {
            print(LocalizedError.self)
        }


    }

    func completenItem(_ indexPath:IndexPath) {
        var nItem = nItems[indexPath.row]
        nItem.markAsCompleted()
        nItems[indexPath.row] = nItem


        let createdDate = nItem.createdAt
        let formatter = DateFormatter()
        formatter.dateFormat = "MMM d, yyyy hh:mm a"
        formatter.dateStyle = .medium
        formatter.timeStyle = .medium
        let finalDate = formatter.string(from: createdDate)




        if let cell = tableView.cellForRow(at: indexPath) as? nTableViewCell{
              cell.nPurposeLabel.attributedText  = strikeThroughText(nItem.title)
              cell.nsDateOutlet.attributedText  = strikeThroughText(finalDate)
            UIView.animate(withDuration: 0.1, animations: {
                cell.transform = cell.transform.scaledBy(x: 1.5, y: 1.5)
            }) { (success) in
                UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
                    cell.transform = CGAffineTransform.identity
                }, completion: nil)
            }
        }
//        if let indexPath = tableView.indexPath(for: cell){
//        var nItem = nItems[indexPath.row]
//        nItem.markAsCompleted()
//        cell.nPurposeLabel.attributedText  = strikeThroughText(nItem.title)
    }



    func strikeThroughText (_ text:String) -> NSAttributedString{
        let strokeEffect: [NSAttributedString.Key : Any] =
            [
                NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
                NSAttributedString.Key.strikethroughColor: (UIColor(red: 0.0, green: 1.0, blue: 1.0, alpha: 1.0))
                ]

        let attributeString = NSAttributedString(string: text, attributes: strokeEffect)
        return attributeString
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
        tableView.reloadData()
        progressBar.setProgress(progress, animated: true)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        tableView.reloadData()
        loadData()
    }
    func loadData() {
        nItems = [TodoItem]()
        nItems = nDataManager.loadAll(TodoItem.self).sorted(by: {
            $0.createdAt < $1.createdAt
        })
        tableView.reloadData()
//        progressBar.setProgress(progress, animated: true)
    }
    // MARK: - Table view data source



    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1   }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



        return nItems.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "nCell", for: indexPath) as! nTableViewCell



        let item = nItems[indexPath.row]


        let createdDate = item.createdAt
        let formatter = DateFormatter()
        formatter.dateFormat = "MMM d, yyyy hh:mm a"
        formatter.dateStyle = .medium
        formatter.timeStyle = .medium

        let finalDate = formatter.string(from: createdDate)

         cell.nPurposeLabel.text = item.title
         cell.nsDateOutlet.text = finalDate
        if item.completed {

            cell.nPurposeLabel.attributedText = strikeThroughText(item.title)
            cell.nsDateOutlet.attributedText = strikeThroughText(finalDate)
        }
        return cell
    }




    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (action: UITableViewRowAction, indexPath:IndexPath) in
            self.nItems[indexPath.row].deleteItem()
            self.nItems.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .automatic)
        }
            let completeAction = UITableViewRowAction(style: .normal, title: "Complete") { (action: UITableViewRowAction, indexPath:IndexPath) in
                self.completenItem(indexPath)
            }

        if #available(iOS 11.0, *) {
            deleteAction.backgroundColor = #colorLiteral(red: 1, green: 0, blue: 0.150029252, alpha: 1)
            completeAction.backgroundColor = #colorLiteral(red: 0.1676996052, green: 0.1956448257, blue: 0.2248058021, alpha: 1)
        } else {

            deleteAction.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
        }
        return [deleteAction ,completeAction]
    }

View Controller 2:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nsVC = segue.destination as! nTableViewController
        nsVC.selectedTitle = selectednsTitle
    }

    func action (with selectednsTitle:String){

        performSegue(withIdentifier: "unwindtonsTBVCWithSegue", sender: self)

}

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
N H
  • 1
  • 2

0 Answers0