无法调用" index"使用类型'(of:ChecklistItem)的参数列表'

时间:2017-09-07 23:15:02

标签: ios swift

我不理解Xcode在将项放入数组时返回此错误,并为ChecklistItem对象设置变量。我已经评论了返回错误的行。

import UIKit

class ChecklistViewController: UITableViewController, AddItemViewControllerDelegate {

    var items: [ChecklistItem]

    required init?(coder aDecoder: NSCoder) {
        items = [ChecklistItem]()

        let row0item = ChecklistItem()
        row0item.text = "Walk the dog"
        row0item.checked = false
        items.append(row0item)

        let row1item = ChecklistItem()
        row1item.text = "Brush my teeth"
        row1item.checked = true
        items.append(row1item)

        let row2item = ChecklistItem()
        row2item.text = "Learn iOS development"
        row2item.checked = true
        items.append(row2item)

        let row3item = ChecklistItem()
        row3item.text = "Soccer practice"
        row3item.checked = false
        items.append(row3item)

        let row4item = ChecklistItem()
        row4item.text = "Eat ice cream"
        row4item.checked = true
        items.append(row4item)

        super.init(coder: aDecoder)
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


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

        return items.count

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath)

        let item = items[indexPath.row]

        let label = cell.viewWithTag(1000) as! UILabel
        label.text = item.text

        configureText(for: cell, with: item)
        configureCheckmark(for: cell, with: item)

        return cell

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if let cell = tableView.cellForRow(at: indexPath) {
            let item = items[indexPath.row]
            item.toggleChecked()
            item.checked = !item.checked

            configureCheckmark(for: cell, with: item)
        }
        tableView.deselectRow(at:indexPath, animated: true)
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        items.remove(at: indexPath.row)

        let indexPaths = [indexPath]
        tableView.deleteRows(at: indexPaths, with: .automatic)
    }

    func configureCheckmark(for cell: UITableViewCell, with item: ChecklistItem) {

        let label = cell.viewWithTag(1001) as! UILabel


        if item.checked {
            label.text = "√"
        } else {
            label.text = " "
        }
    }

    func configureText(for cell: UITableViewCell, with item: ChecklistItem) {

        let label = cell.viewWithTag(1000) as! UILabel
        label.text = item.text

    }

    func addItemViewControllerDidCancel(_ controller: AddItemViewController) {
        dismiss(animated: true, completion: nil)
    }

    func addItemViewController(_ controller: AddItemViewController, didFinishAdding item: ChecklistItem) {

        let newRowIndex = items.count
        items.append(item)

        let indexPath = IndexPath(row: newRowIndex, section: 0)
        let indexPaths = [indexPath]
        tableView.insertRows(at: indexPaths, with: .automatic)

        dismiss(animated: true, completion: nil)
    }

    func addItemViewController(_ controller: AddItemViewController, didFinishEditing item: ChecklistItem) {

        if let index = items.index(of: item) { // Cannot invoke "index" with an argument list of type'(of: ChecklistItem)'
            let indexPath = IndexPath(row: index, section: 0)
            if let cell = tableView.cellForRow(at: indexPath) {
                configureText(for: cell, with: item)
            }
        }
        dismiss(animated: true, completion: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "AddItem" {

            let navigationController = segue.destination as! UINavigationController
            let controller = navigationController.topViewController as! AddItemViewController

            controller.delegate = self
        } else if segue.identifier == "EditItem" {

            let navigationController = segue.destination as! UINavigationController
            let controller = navigationController.topViewController as! AddItemViewController

            controller.delegate = self

            if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) {
                controller.itemToEdit = items[indexPath.row]
            }


        }
    }



}

ChecklistItem.swift

导入基金会

class ChecklistItem {
    var text = ""
    var checked = false

    func toggleChecked() {
        checked = !checked
    }
}

1 个答案:

答案 0 :(得分:0)

您需要符合Equatable协议.. 任何可能谷歌事先。