[英]How to force a UIView to update
我正在一個正在從服務器下載JSON數據,然后將其顯示在UIView上的應用程序上。 該視圖確實得到更新,只需要15秒鍾。 我已經進行測試,以確保不是下載延遲了更新,並且如果在添加數據時更改視圖的背景色,它也會等待更新。 我是否認為視圖未更新是正確的,還是其他? 如何強制更新視圖? 謝謝。
var newView = CustomView(product: products[0],descriptive: false)
contentView.addSubview(newView)
let backHeight = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 0.2, constant: 0)
let backWidth = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
let backXPosition = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant:0 )
let backYPosition = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: colorLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
view.addConstraints([backHeight,backWidth,backXPosition,backYPosition])
這就是我將項目添加到視圖的方式。
您是否具有在視圖中顯示數據的代碼示例?
您可以通過調用觸發視圖更新
view.setNeedsDisplay() //Updates any drawing, including that in drawRect()
和
view.setNeedsLayout() //Triggers layoutSubviews()
但是我認為這兩種方法都不會有幫助,好像您要添加新視圖而無需調用這些視圖那樣。 除非您使用Dharmesh建議的其他線程。
確保將視圖添加到父視圖,並確保也在其上設置框架,就好像它們是標簽一樣,如果未設置框架,它們可能不會出現。
從服務器獲取數據后,可以使用主隊列將UI更新到視圖中,如以下代碼所示:
dispatch_async(dispatch_get_main_queue()) {
//Update your UI here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.