Home>

I made a simple Todo app.

As I just added a task as shown in the image above, I want to delete it because more and more tasks accumulate.

Therefore, I found an article like this on the internet,
(Quoted: https://qiita.com/mironal/items/bb0ab5c42fe687b301ec )

In the article,

// where you pressed delete
let index = indexPath.row
items.removeAtIndex (index)
tableView.reloadData ()
It was written that it can be deleted with

.

I added to the Todo app code, but the following error occurred.

cell_array.removeAtIndex (index) ← Value of type '[String]' has no member 'removeAtIndex'

You probably say that [String] (), the type of the array that stores the task, does not have removeAtIndex.

However, even if the array is a [String] type, it is in AddController.

cell_array.append (textField.text!)
where
Cannot convert value of type 'String' to expected argument type 'inout Array<_>'
The error

will occur.

I want to be able to delete tasks by swiping like the reminder app of the Mac standard app.

I would like to present a solution.

Thank you.

The following is the source code.

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        // return the number of cells
        return cell_array.count
    }
    func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        // Create a cell variable
        let cell: UITableViewCell = tableView.dequeueReusableCell (withIdentifier: "TodoCell", for: indexPath)
        // Store the contents of the cell registered in UD
        cell.textLabel! .text = cell_array [indexPath.row]
        // return the cell to display
        return cell
    }
    // Delete the task
    func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        let index = indexPath.row
        cell_array.removeAtIndex (index) // An error occurred here
        tableView.reloadData ()
    }
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
        // Receive contents stored in UD
        if UserDefaults.standard.object (forKey: "TodoList")! = nil {
            cell_array = UserDefaults.standard.object (forKey: "TodoList") as! [String]
        }
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }
}
import UIKit
// array to store variables
var cell_array = [String] ()
class AddController: UIViewController {
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var textField: UITextField!
    @IBAction func addButton (_ sender: Any) {
        // Add the entered text
        cell_array.append (textField.text!)
        // Delete the textField value
        textField.text = ""
        // Register to UD
        UserDefaults.standard.set (cell_array, forKey: "TodoList")
    }
  • Answer # 1

    The reference information seems old.

    // Delete task
        func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
            let index = indexPath.row
            //cell_array.removeAtIndex(index) // An error occurred here
            // ↓ How about this?
            cell_array.remove (at: index)
            tableView.reloadData ()
        }