不工作委托 UITableViewController

时间:2021-04-20 06:42:45

标签: ios swift uitableview

我以编程方式创建了 UITableViewController,但是删除行的方法editingStyle 和 didSelectRowAt 不起作用。我无法弄清楚为什么它们不起作用。我检查了一切并尝试了无论如何委托方法不起作用。

我的代码:

import UIKit

class FPTasksViewController: UITableViewController, FPPopUpViewControllerDelegate {

    let date = String(DateFormatter.localizedString(from: NSDate() as Date, dateStyle: .long, timeStyle: .none))

    private var tasks: [FPTask] = FPDefaults.sh.tasks {
        didSet {
            FPDefaults.sh.tasks = self.tasks
        }
    }

    // MARK: - life cycle functions

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.separatorStyle = .none

        let tapGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didTapTableView))

        tableView.addGestureRecognizer(tapGestureRecognizer)
    }

    // MARK: - actions

    func FPPopUpViewControllerOkButtonTapped(_ controller: FPPopUpViewController, didFinishAdding newTask: FPTask) {
        let newRowIndex = tasks.count
        self.tasks.append(newTask)

        let indexPath = IndexPath(row: newRowIndex, section: 0)

        tableView.insertRows(at: [indexPath], with: .automatic)
        navigationController?.popViewController(animated: true)
    }

    @objc func addButtonTapped() {
        let popUp = FPPopUpViewController()
        popUp.delegate = self

        self.view.addSubview(popUp)

        self.view.backgroundColor = UIColor.systemGray3.withAlphaComponent(0.8)
    }

    @objc func didTapTableView(_ gestureRecognizer: UILongPressGestureRecognizer) {
        self.tableView.backgroundColor = .black
    }

    // MARK: - table view

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        switch editingStyle {
        case .delete:
            self.tasks.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        default:
            break
        }
    }

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

        return self.tasks.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: FPTasksCell.reuseIdentifier,
                                                 for: indexPath) as? FPTasksCell ?? FPTasksCell()

        let task = tasks[indexPath.row]

        cell.setCellData(taskName: "  \(task.taskTitle)", taskDescription: "  from \(date.lowercased())")

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(1)
    }
}

为什么我不能删除行?以及如何使 didSelectRowAt 方法也起作用?我做错了什么?

2 个答案:

答案 0 :(得分:1)

在 viewDidLoad 方法中设置 tableView 数据源和委托。

self.tableView.delegate = self
self.tableView.dataSource = self

答案 1 :(得分:0)

class FPTasksViewController: UITableViewController, FPPopUpViewControllerDelegate, UITableViewDataSource, UITableViewDelegate {
    
         override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
    
             self.tableView.delegate = self
             self.tableView.dataSource = self
        }
    
    }