堆栈视图中仅显示最后存储的数据
Only the last stored data is shown in a stack view
我正在进行 API 调用,其中 return 一组我想在屏幕上显示的键值对。我正在使用一个 xib 文件并向其中添加键值对。显示屏上仅显示最后一个键值对。这是使用 MVVM 架构完成的。
@IBOutlet var currentBenefitsStackView: UIStackView!
func populateMemberBenefits(_ data: ResponseData) {
let nib = UINib(nibName: "StackView", bundle: Bundle.SampleAccount)
if let memberBenefitsView = nib.instantiate(withOwner: nil, options: nil).first as? StackView {
guard let memberBenefitsPerks = data.benifts else {return} //data.benifts contains the key value pairs
for benefits in memberBenefitsPerks {
memberBenefitsView.labelText?.text = benefits.value
let imageURL = URL(string: "\(SampleURL)")
let image = try? Data(contentsOf: imageURL!)
memberBenefitsView.imageView.image = UIImage(data: image!)
currentBenefitsStackView.addArrangedSubview(memberBenefitsView)
}
在 currentBenefitsStackView 中仅显示最后一个 memberBenefitsView 图像。任何人都可以指出我在哪里犯了错误或者是否需要任何进一步的信息。
您只分配了一个 memberBenefitsView
并不断更新它。它将保留分配的最终值。
您需要在 for
循环的每次迭代中分配一个视图:
func populateMemberBenefits(_ data: ResponseData) {
let nib = UINib(nibName: "StackView", bundle: Bundle.SampleAccount)
guard let memberBenefitsPerks = data.benifts else {return} //data.benifts contains the key value pairs
for benefits in memberBenefitsPerks {
if let memberBenefitsView = nib.instantiate(withOwner: nil, options: nil).first as? StackView {
memberBenefitsView.labelText?.text = benefits.value
let imageURL = URL(string: "\(SampleURL)")
let image = try? Data(contentsOf: imageURL!)
memberBenefitsView.imageView.image = UIImage(data: image!)
currentBenefitsStackView.addArrangedSubview(memberBenefitsView)
}
}
}
我正在进行 API 调用,其中 return 一组我想在屏幕上显示的键值对。我正在使用一个 xib 文件并向其中添加键值对。显示屏上仅显示最后一个键值对。这是使用 MVVM 架构完成的。
@IBOutlet var currentBenefitsStackView: UIStackView!
func populateMemberBenefits(_ data: ResponseData) {
let nib = UINib(nibName: "StackView", bundle: Bundle.SampleAccount)
if let memberBenefitsView = nib.instantiate(withOwner: nil, options: nil).first as? StackView {
guard let memberBenefitsPerks = data.benifts else {return} //data.benifts contains the key value pairs
for benefits in memberBenefitsPerks {
memberBenefitsView.labelText?.text = benefits.value
let imageURL = URL(string: "\(SampleURL)")
let image = try? Data(contentsOf: imageURL!)
memberBenefitsView.imageView.image = UIImage(data: image!)
currentBenefitsStackView.addArrangedSubview(memberBenefitsView)
}
在 currentBenefitsStackView 中仅显示最后一个 memberBenefitsView 图像。任何人都可以指出我在哪里犯了错误或者是否需要任何进一步的信息。
您只分配了一个 memberBenefitsView
并不断更新它。它将保留分配的最终值。
您需要在 for
循环的每次迭代中分配一个视图:
func populateMemberBenefits(_ data: ResponseData) {
let nib = UINib(nibName: "StackView", bundle: Bundle.SampleAccount)
guard let memberBenefitsPerks = data.benifts else {return} //data.benifts contains the key value pairs
for benefits in memberBenefitsPerks {
if let memberBenefitsView = nib.instantiate(withOwner: nil, options: nil).first as? StackView {
memberBenefitsView.labelText?.text = benefits.value
let imageURL = URL(string: "\(SampleURL)")
let image = try? Data(contentsOf: imageURL!)
memberBenefitsView.imageView.image = UIImage(data: image!)
currentBenefitsStackView.addArrangedSubview(memberBenefitsView)
}
}
}