【发布时间】:2023-01-19 03:03:38
【问题描述】:
在我的应用程序中,我存储了一个具有不同手指测量值的变量。不同的手指测量值在 UITableViewCell 中有值,但在 SwiftUI 视图中显示为 nil。
在 UITableViewCell 中,我将变量设为已发布变量。然后我去了 SwiftUI 视图并尝试了@ObservedObject,然后在@ObservedObject 不起作用时尝试了@StateObject。同样,UITableViewCell 中的日志显示值,但 SwiftUI 视图中的日志显示为 nil,默认为 0。
以下是发布的 var 代码的 sn-ps 以及类型。然后是我尝试传递变量的 SwiftUI 视图的 sn-p。
struct SaveNailImagesResponse: Codable {
var id: Int?
var leftIndexLength: Double?
var leftIndexSize: Int?
var leftIndexWidth: Double?
var leftMiddleLength: Double?
var leftMiddleSize: Int?
var leftMiddleWidth: Double?
var leftPinkyLength: Double?
var leftPinkySize: Int?
var leftPinkyWidth:
}
/// SNIPPET OF UITABLEVIEWCELL
class HandMeasurementsTableViewCell: UITableViewCell, ObservableObject {
@Published var fingerSizeData: SaveNailImagesResponse? {
didSet {
self.tableView.reloadData()
print(self.fingerSizeData?.leftIndexSize as Any)
print(self.fingerSizeData?.leftRingSize as Any)
print(self.fingerSizeData?.leftMiddleSize as Any)
}
}
}
/// HERE IS SNIPPET OF SWIFTUI VIEW: I tried passing it a 2 different ways below but comes up as nil and defaults to 0 every time
struct ResultsThumbnailView1: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var data: HandMeasurementsTableViewCell = HandMeasurementsTableViewCell()
var body: some View {
VStack {
Text(String(self.data.fingerSizeData?.leftRingSize ?? 0))
Text("Size \(String(data.fingerSizeData?.leftIndexSize ?? 0))")
.font(.system(size: 19, weight: .bold))
}
}
}
// VIEW CONTROLLER WHERE EVERYTHING CONNECTS
class ResultsSizesViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var resultsData: SaveNailImagesResponse?
private var thumbnailButton: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(systemName: "camera.circle"), for: .normal)
button.tintColor = .black
button.addTarget(self, action: #selector(presentThumbnailPage), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
["HandMeasurementsTableViewCell"].forEach( {
tableView.register(UINib.init(nibName: $0, bundle: nil), forCellReuseIdentifier: $0)
})
tableView.delegate = self
tableView.dataSource = self
setNavBar(backButtonAvailable: true, title: "Results")
setUI()
thumbnailButtonConstraints()
}
@objc func presentThumbnailPage() {
let vc = UIHostingController(rootView: ResultsThumbnailTabView())
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HandMeasurementsTableViewCell") as? HandMeasurementsTableViewCell
let data = self.resultsData
cell?.fingerSizeData = data
if indexPath.row == 0 {
cell?.titleLabel.text = "Left Hand"
} else if indexPath.row == 1 {
cell?.titleLabel.text = "Right Hand"
}
return cell!
}
func thumbnailButtonConstraints() {
view.addSubview(thumbnailButton)
thumbnailButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
thumbnailButton.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20),
thumbnailButton.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
thumbnailButton.heightAnchor.constraint(equalToConstant: 20),
thumbnailButton.widthAnchor.constraint(equalToConstant: 20),
])
thumbnailButton.addTarget(self, action: #selector(presentThumbnailPage), for: .touchUpInside)
}
}
【问题讨论】:
-
无法判断,因为您没有提供足够的代码来重现甚至显示所有内容是如何连接的,但是每次您调用
HandMeasurementsTableViewCell()时,您都会创建一个不同的实例,一个不了解另一个,并且缩略图视图没有连接到任何东西. -
抱歉,我希望看看我是否在 SwiftUI 视图中调用了错误的变量。我更新了上面的代码并显示我正在正确使用 UIHosting 控制器以及 UITableViewCell 的连接方式@loremipsum