在表格视图中自动敲击标签文本

时间:2019-01-13 20:04:45

标签: ios swift uitableview viewcontroller

我有两个表视图控制器,第二个表视图控制器包含具有不同选项的条目,以添加到第一个视图表视图控制器。 表格视图控制器的第一行有两个滑动动作,即完成和删除。

每行/单元格有两个标签,一个带有从第二个表格视图控制器中选择的选项的标题,第二个标签是其创建的日期。

点击完成的滑动动作后,删除线功能将同时删除两个标签的文本。

但是,我注意到每次滑动条目并将其删除,然后再次添加相同条目时,它都会自动滑动单元格的标题标签。

如果我返回并重新启动第一个视图控制器,它将清除警告。

这种罢工是随机发生的,即有时罢工,有时不罢工,甚至不调用穿越功能。

任何建议都将不胜感激。

查看控制器1:

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

0 个答案:

没有答案