gpt4 book ai didi

ios - NSObject 在加载 viewDidLoad() 之前似乎没有初始化

转载 作者:行者123 更新时间:2023-11-29 05:33:42 25 4
gpt4 key购买 nike

对新手问题表示歉意,但我真的很挣扎...我在尝试使用 NSObject 的自定义类创建 tableView 数据源时遇到问题。我已经设法在以前的 View Controller 中实现了这一点,但是当我尝试复制它时,它似乎不起作用。看起来 init() 函数不会在 tableview 上的 viewDidLoad() 之前完成,因此数据仅在显示表后添加到源中。

我能看到的唯一区别是 tableView 所在的 viewController 是 UITabBarController 的一部分...

这是当前代码...

EventTableViewController.swift

import UIKit
import Foundation

class EventTableViewController: UIViewController {

@IBOutlet var tableView: UITableView!

var eventModel = EventModel()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
tableView.dataSource = eventModel
tableView.delegate = eventModel

self.tableView.reloadData()
print("Table already loaded")

}
}

EventModel.swift

import Foundation
import UIKit

class Event {

let eventName: String
let eventType: String

let eventStartDate: Date
let eventEndDate: Date

let participants: [String]

init(item: PFObject) {
self.eventName = (item["eventName"] as? String)!
self.eventType = (item["eventType"] as? String)!
self.eventStartDate = (item["startDate"] as? Date)!
self.eventEndDate = (item["endDate"] as? Date)!
self.participants = (item["eventParticipants"] as? [String])!
}
}

class EventModel: NSObject {

var items = [Event]()

override init() {
// Setup the Events
super.init()
items = getAndConvertEvents()
}


func getAndConvertEvents() -> [Event] {

var events = [Event]()
let currentUser = PFUser.current()

// Use this to get the events...!
let query = PFQuery(className:"Events")
query.whereKey("eventParticipants", contains: currentUser?.username)
query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
// Log details of the failure
print(error.localizedDescription)
} else if let objects = objects {
// The find succeeded.
print("Successfully retrieved \(objects.count) events for user.")
// Do something with the found objects
for object in objects {
print(object.objectId as Any)
}
events = objects.map { Event.init(item: $0) }
}
}
return events
}
}

extension EventModel: UITableViewDataSource {

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

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

// Set the table parameters
cell.nameLabel.text = items[indexPath.row].eventName

}
return UITableViewCell()
}
}

extension EventModel: UITableViewDelegate {

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {

return indexPath
}
}

有什么办法可以阻止 viewDidLoad() 在 EventModel 完成加载之前完成吗?

更新

我已经包含了我的 appdelegate 调用来初始化 UITabBarController

AppDelegate.swift

import UIKit
import Parse
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let configuration = ParseClientConfiguration {
$0.applicationId = ""
$0.clientKey = ""
$0.server = ""
$0.isLocalDatastoreEnabled = true
}
Parse.initialize(with: configuration)

let currentUser = PFUser.current()
if currentUser != nil {
// Do stuff with the user
// Load the Event View as user is logged in
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EventTabController") as! UITabBarController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

} else {
// Show the signup or login screen
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "LoginView") as! StartVerificationViewController
self.window?.rootViewController = homePage
}


return true
}
}

最佳答案

@maddy 你是对的......!我在解析服务器调用中调用异步进程......!一如既往,我应该发布整个代码 - 抱歉,我什至不认为这会是问题!

我现在已更新为进行同步调用

    func getAndConvertEvents() -> [Event] {

var events = [Event]()

let currentUser = PFUser.current()

// Use this to get the events...!
let query = PFQuery(className:"Events")
query.whereKey("eventParticipants", contains: currentUser?.username)
do {
let objects = try query.findObjects()
print("Successfully retrieved \(objects.count) events for user.")
// Do something with the found objects
for object in objects {
print(object.objectId as Any)
}
events = objects.map { Event.init(item: $0) }
} catch {
print(error.localizedDescription)
}
return events
}

关于ios - NSObject 在加载 viewDidLoad() 之前似乎没有初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com