如何在 iOS 中重新加载 UiView?
How to reload a UiView in iOS?
我想重新加载 UiView 中的数据。
我经常使用此代码重新加载 tableview 中的数据:
tableview.reloadData
有什么方法可以像tableView一样在uiView中重新加载数据吗?
视情况而定。可能显示大量数据(如 UITableView
或 UIPickerView
)的典型“复杂”视图使用 数据源 协议,允许它们按需获取数据。在这种情况下,您需要通知视图新的/修改的数据可用,因此调用 reloadData
。然后,视图会向数据源询问要显示的数据。
原始视图(如UILabel
)只是提供一个属性(如text
),你需要设置/更新,然后视图将重新显示数据。
这里想介绍一下View Drawing Cycle
The UIView class uses an on-demand drawing model
for presenting content. When a view first
appears on the screen, the system asks it to draw its content
. The system captures a snapshot of this content
and uses that snapshot as the view’s visual representation. If you never change the view’s content, the view’s drawing code may never be called again. The snapshot image is reused
for most operations involving the view. If you do change the content, you notify
the system that the view has changed. The view then repeats the process of drawing the view and capturing a snapshot of the new results.
当视图的内容发生变化时,您不会直接重绘这些变化。相反,您 invalidate the view
使用 setNeedsDisplay
或 setNeedsDisplayInRect:
方法。这些方法告诉系统视图的内容已更改,需要在下一次机会时重新绘制。系统会等到当前 运行 循环结束才开始任何绘图操作。这种延迟使您有机会一次使多个视图无效、从层次结构中添加或删除视图、隐藏视图、调整视图大小和重新定位视图。您所做的所有更改都会同时反映出来。
如果为视图更改这些 properties,则不必触发 setNeedsDisplay()
或 setNeedsDisplay(_:)
。如果在某些情况下确实想要这样做,您始终可以通过调用 setNeedsDisplay()
或 setNeedsDisplay(_:)
方法强制重绘视图的内容。
你可以试试:
self.tableView.reloadData()
要么
tableView?.reloadData()
我认为您正在从服务器获取 JSON 数据,这是一项异步任务,并且您在服务器对您做出响应之前在标签中设置数据,这看起来数据未填充在标签。使用escaping closure在服务器响应您后更新标签中的数据。
我想重新加载 UiView 中的数据。 我经常使用此代码重新加载 tableview 中的数据:
tableview.reloadData
有什么方法可以像tableView一样在uiView中重新加载数据吗?
视情况而定。可能显示大量数据(如 UITableView
或 UIPickerView
)的典型“复杂”视图使用 数据源 协议,允许它们按需获取数据。在这种情况下,您需要通知视图新的/修改的数据可用,因此调用 reloadData
。然后,视图会向数据源询问要显示的数据。
原始视图(如UILabel
)只是提供一个属性(如text
),你需要设置/更新,然后视图将重新显示数据。
这里想介绍一下View Drawing Cycle
The UIView class uses an
on-demand drawing model
for presenting content. When a viewfirst
appears on the screen, the system asks it todraw its content
. The systemcaptures a snapshot of this content
and uses that snapshot as the view’s visual representation. If you never change the view’s content, the view’s drawing code may never be called again. The snapshot image isreused
for most operations involving the view. If you do change the content, younotify
the system that the view has changed. The view then repeats the process of drawing the view and capturing a snapshot of the new results.
当视图的内容发生变化时,您不会直接重绘这些变化。相反,您 invalidate the view
使用 setNeedsDisplay
或 setNeedsDisplayInRect:
方法。这些方法告诉系统视图的内容已更改,需要在下一次机会时重新绘制。系统会等到当前 运行 循环结束才开始任何绘图操作。这种延迟使您有机会一次使多个视图无效、从层次结构中添加或删除视图、隐藏视图、调整视图大小和重新定位视图。您所做的所有更改都会同时反映出来。
如果为视图更改这些 properties,则不必触发 setNeedsDisplay()
或 setNeedsDisplay(_:)
。如果在某些情况下确实想要这样做,您始终可以通过调用 setNeedsDisplay()
或 setNeedsDisplay(_:)
方法强制重绘视图的内容。
你可以试试:
self.tableView.reloadData() 要么 tableView?.reloadData()
我认为您正在从服务器获取 JSON 数据,这是一项异步任务,并且您在服务器对您做出响应之前在标签中设置数据,这看起来数据未填充在标签。使用escaping closure在服务器响应您后更新标签中的数据。