ios - 通过委托(delegate)将数据传递给 UITableView

标签 ios swift uitableview delegates

在过去的几天里,我一直在努力将数据传递给各种 View Controller 。 一开始我跳得太深了,变得很困惑。 最后我想我已经排序了。但是我看不到下面哪里出了问题。我很接近,但我认为我在周末尝试了这么多东西而失明了。查看了回调,但我认为委托(delegate)对我来说更有意义。

我有 2 个 View Controller 。一种带有 UITableView,一种通过文本字段输入数据。 我没有错误。然而,输入打印在 viewcontroller 2 中,但未显示在 UITableView 中。 发现很难看出我做错了什么。

VC1:

import Foundation
import UIKit


private let reuseidentifier = "Cell"

//here
struct Contact {
var fullname: String
}





class ContactController: UITableViewController {

var contacts = [Contact]()


override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationItem.title = "Contacts"

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handleAddContact))
    view.backgroundColor = .white
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseidentifier)
}

@objc func handleAddContact () {

    //here
    let controller = AddContactController()
    controller.delegate = self

    self.present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
}


//UITABLEVIEW
override func numberOfSections(in tableView: UITableView) -> Int {
    return contacts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseidentifier, for: indexPath)
    cell.textLabel?.text = contacts[indexPath.row].fullname
    return cell
}

}

  //here
  extension ContactController: AddContactDelegate {

func addContact(contact: Contact) {
    self.dismiss(animated: true) {
        self.contacts.append(contact)
        self.tableView.reloadData()
    }
}

}

输入数据的我的VC2

import Foundation
import UIKit

//here
protocol AddContactDelegate {
func addContact(contact: Contact)
 }

 class AddContactController: UIViewController {


//here
var delegate: AddContactDelegate?

let textField: UITextField = {
    let tf = UITextField()
    tf.placeholder = "Full Name"
    tf.textAlignment = .center
    tf.translatesAutoresizingMaskIntoConstraints = false
    return tf
}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .white


   self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDone))

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(handleCancel))

    view.addSubview(textField)
    textField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    textField.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    textField.widthAnchor.constraint(equalToConstant: view.frame.width - 64).isActive = true

    textField.becomeFirstResponder()

}

//here
@objc func handleDone(){
print("done")

    guard let fullname = textField.text, textField.hasText else {
        print("handle error here")
        return
    }

    let contact = Contact(fullname: fullname)
    delegate?.addContact(contact: contact)
    print(contact.fullname)
}


@objc func handleCancel(){
    self.dismiss(animated: true, completion: nil )
}

}

最佳答案

你的委托(delegate)实现没有问题,你需要实现numberOfRowsInSectionreturn 1

override func numberOfSections(in tableView: UITableView) -> Int {
     return contacts.count
}       
override func tableView(_ tableView: UITableView, 
      numberOfRowsInSection section: Int) -> Int {
    return 1
 }

或者想想如果你真的只需要这个

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

关于ios - 通过委托(delegate)将数据传递给 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59448285/

相关文章:

swift - 无法构建或存档运行良好的 Xcode 项目

ios - 在第四代 iPad 上使用 Eureka Pod

ios - UITableView.setEditing() 无法禁用编辑模式

ios - 如何在 Swift 中动态创建 buttonAction 函数

ios - 如何删除 iOS 13 中带有外观的导航栏背景?

iphone - 如何将图像放入 iPad 上的 UIImagePickerController

arrays - Swift - 将数据加载到结构数组中

ios - 如何回收 UICollectionViewCell 或 UITableViewCell 的背景 View

ios - 获取多维数组的键( `[String: [String: String]]`)

ios - iOS 8 之后,我可以继续使用 UIActionSheet 和 UIAlertView 吗?