swift - 使用相同的reuseidentifier重新加载各种 TableView

标签 swift ios10 nsnotificationcenter uitableview reloaddata

我有几个常见的“表格 View ”,其单元格具有相同的名称:"LiveViewCell" ,我想重新加载 view controller 中创建的数据。 只剩下一个view controller得到 reloadData()调用 notification ,甚至当前发送数据的人也无法获取到它。

这是我在所有 View Controller 中实现的代码:

@IBOutlet weak var tableViewLiveCells: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(self.reloadLive),name:NSNotification.Name(rawValue: "load"), object: nil)
}

func reloadLive(notification: NSNotification) {
    tableViewLiveCells.reloadData()
}

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

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableViewLiveCells.dequeueReusableCell(withIdentifier: "LiveViewCell", for: indexPath) as! LiveViewCell

    let tab: Table!

    tab = NT[indexPath.row]

    cell.configureLiveCell(tab)

    return cell
}

这是LiveViewCell.swift文件

import UIKit


class LiveViewCell: UITableViewCell {


    @IBOutlet weak var tableNumberLeftLabel: UILabel!
    @IBOutlet weak var guestNumbersLabel: UILabel!

    @IBOutlet weak var timeInTableLabel: UILabel!
    @IBOutlet weak var tableNumberLbl: UILabel!
    @IBOutlet weak var timeRemainingLbl: UILabel!


    var table: Table!


    func configureLiveCell(_ NT: Table) {

        layer.cornerRadius = 20

        tableNumberLbl.text = "T" + String(NT.number)
        timeRemainingLbl.text = String(time)


    }

    func configureCell(_ NT: Table) {

        tableNumberLeftLabel.text = "T" + String(NT.number)
        guestNumbersLabel.text = "COVERS:" + String(NT.guests)
        timeInTableLabel.text = "TIME IN:" + NT.timeIn

    }



}

从创建要重新加载的数据的 viewController:

import UIKit

var NT = Table.MyTables.newTable


class NewTableVC: UIViewController {


    @IBOutlet weak var tableViewLiveCells: UITableView!

    @IBOutlet weak var imageTableCreated: UIImageView!

    @IBOutlet weak var tableNumberTF: UITextField!
    @IBOutlet weak var guestNumberTF: UITextField!
    @IBOutlet weak var timeLabelTable: UILabel!
    @IBOutlet weak var prebooked: UISwitch!
    @IBOutlet weak var dateLabel: UILabel!

    @IBAction func createButton(_ sender: AnyObject) {

        print("createButton tapped")

        let newNumber = Int(tableNumberTF.text!)
        let newGuestNumber = Int(guestNumberTF.text!)
        let currentTimeArrival:String? = timeLabelTable.text

        if (tableNumberTF.text?.isEmpty)! || (guestNumberTF.text?.isEmpty)! {
            let alert = UIAlertController(title: "Missing input(s)", message: "Please fill in all the gaps", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
            alert.addAction(UIAlertAction(title: "Back Home", style: .default, handler: {(alert: UIAlertAction) in

                self.dismiss(animated: true, completion: nil)


            }))
            self.present(alert, animated: true, completion: nil)

        } else {

            let presentTable = Table(number: newNumber!, guests: newGuestNumber!, timeIn: currentTimeArrival!)

            NT.append(presentTable)

            print("This is a new table with the following values")
            print(presentTable.number)
            print(presentTable.guests)
            print(presentTable.timeIn)
            print(NT.count)

            imageTableCreated.alpha = 1
            imageTableCreated.layer.zPosition = 500

            tableViewLiveCells.reloadData()

            let deadline = DispatchTime.now() + .seconds(1)
            DispatchQueue.main.async {
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
            }
            DispatchQueue.main.asyncAfter(deadline: deadline) {
                self.imageTableCreated.alpha = 0
                self.imageTableCreated.layer.zPosition = -500

            }

        }
    }
}

reloadData()不适用于任何 viewController除了上一个到此 NewTableVC viewController创建新数据,这是我在创建其他数据后创建的第一个数据。 该问题是否与复制和粘贴相同内容有关tableViewLiveCells到所有其他 View Controller ? 如果是这样,我尝试重新创建表格 View ,但它不允许我再次连接标签。

最佳答案

在代码中设置断点并确保通知已发布,然后检查响应这些通知的每个 View 是否都能看到该通知。

您的通知设置正确,但当您的方法名称为 reloadLive 时,您一直指出 reloadData() 不适用于您的 View Controller 。我还会仔细检查所有 View 设置,它们在 viewDidLoad 中响应哪些通知,就像您在上面的第一个 View Controller 中一样,并且它们都有方法 reloadLive,该方法将然后重新加载您的表格 View 。

如果上面的 NewTableVC 代码是该 View Controller 的完整代码,则该 View Controller 尚未添加来监听通知,也没有可调用的方法来响应通知。

关于swift - 使用相同的reuseidentifier重新加载各种 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40309551/

相关文章:

ios - 关于 String 和 c strcpy 的快速问题

ios - UIImage 不在 UIImageView 的所需位置

ios - 如何删除 Storyboard中 UIView 的底部空白区域?

ios - systemgroup.com.apple.configurationprofiles 路径的系统组容器

ios - PhoneGap Build 6.3.0 geolocation getCurrentPosition 在 IOS 上运行缓慢

arrays - 带有 float 组的 Swift 3 扩展

ios - iOS 10 中的错误 : Unable to copy asset information from https://mesu. apple.com/assets/对于 Assets 类型

objective-c - UIImageView 不显示从通知中获取的 UIImage

ios - 从 Unity 桥接到 Swift 时通知中心不调用

xcode - 在 Xcode 单元测试中测量期望时间