ios - tableView reloadData 不起作用,委托(delegate)方法

标签 ios swift uitableview delegates

我正在尝试在 1 个 View Controller (AddCategoryViewController) 中创建新类别并将其显示在 TableView Controller (CategoryViewController) 中。但是重新加载数据时出现问题。

新的类别项目只有在打开和关闭应用程序后才会显示,即使有 tableView.reloadData() 也是如此。

我尝试在 addButtonPressed 函数中更改导航标题,但标题会立即更改。

当我使用 UIAlertView 添加数据时,tableView.reloadData() 起作用了。所以我猜它与 2 个 View Controller 和委托(delegate)方法有关?

感谢您的帮助<3

显示项目:

import UIKit
import CoreData

class CategoryViewController: UITableViewController {

    @IBOutlet weak var navigation: UINavigationItem!

    var categoryArray = [Category]()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    override func viewDidLoad() {
        super.viewDidLoad()

        loadCategory()
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return categoryArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "CategoryItemCell")

        cell.textLabel?.text = categoryArray[indexPath.row].name

        if let randomColor = categoryArray[indexPath.row].color {
            cell.textLabel?.textColor = UIColor(hex: randomColor)
        }

        return cell
    }


    // MARK: - Table view data source
    @IBAction func addPressed(_ sender: UIBarButtonItem) {

        let addCategoryVC = storyboard?.instantiateViewController(withIdentifier: "AddCategoryViewController") as! AddCategoryViewController
        addCategoryVC.delegate = self
        present(addCategoryVC, animated: true, completion: nil)
    }


    // MARK: - CoreData methods

    func saveCategory() {

        do {
            try context.save()

        } catch {
            print("Save error: \(error)")
        }

        tableView.reloadData()

    }

    func loadCategory(with request: NSFetchRequest<Category> = Category.fetchRequest()) {
        do {
            categoryArray = try context.fetch(request)
        } catch {
            print("Load error: \(error)")
        }

        tableView.reloadData()
    }

    func addCategory(name: String, description: String) {

        let newCategory = Category(context: context.self)
        newCategory.name = name
        newCategory.descriptionOfCategory = description
        newCategory.color = UIColor.random().toHex
        saveCategory()

        print("name form func: \(name)")
        print("description from func: \(description)")

    }

}

// MARK: AddCateogry delegate methods

extension CategoryViewController: AddCategoryDelegate {

    func addButtonPressed(name: String, description: String) {
        addCategory(name: name, description: description)
        navigation.title = "I have changed!"
    }
}

添加项目:

import UIKit

protocol AddCategoryDelegate {
    func addButtonPressed(name: String, description: String)
}

class AddCategoryViewController: UIViewController {

    var delegate : AddCategoryDelegate!

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var descriptionTextField: UITextField!

    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.
    }

    @IBAction func addCategoryButtonPressed(_ sender: UIButton) {
        delegate.addButtonPressed(name: nameTextField.text!, description: descriptionTextField.text!)
        dismiss(animated: true, completion: nil)
    }

}

最佳答案

您只需将类别保存到 addCategory 中的 coredata 中,但您还必须将项目添加到数组中,或者在 tableView.reloadData( )saveCategory

关于ios - tableView reloadData 不起作用,委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51966886/

相关文章:

ios - 自定义 UIView 添加目标?

ios - 从 iOS 应用程序在服务器上实现动态搜索的最佳方式是什么?

ios - UITableViewCell 中的 UICollectionView?

ios - 如何实现 Yahoo Weather 的 ScrollView 效果?

ios - 选中时突出显示集合单元格

arrays - 有没有一种简单的方法来计算两个数组之间的差异

swift - 如果让 url = URL(string :"") 对于 .JPEG 图像总是失败

ios - 将 2 个不同的自定义 UITableViewCells 调用到 UITableViewController

iphone - 插入行而不重新加载表

ios - 没有条目的 UITableView