[英]Reload tableview realtime in Swift 3
如何使用timer
實時重新加載tableview
? 如果查看,日志始終是實時的,但在表視圖中則不是實時的。 有什么比我的編碼少的嗎?
@IBOutlet var tableViewChat: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableViewChat = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), style: UITableViewStyle.plain)
startTimer()
}
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(GetOrders), userInfo: nil, repeats: true)
timer.fire()
}
func stopTimer() {
timer.invalidate()
timer = nil
}
func GetOrders() {
getUserOnline()
getChatMessage()
DispatchQueue.main.async {
self.tableViewChat.reloadData()
}
}
嘗試更改表格視圖的高度。 現在它是0,所以我認為它不顯示tableview。 由於您的日志顯示正確,據我所知,表視圖高度應該存在問題。
您的tableViewChat已在情節提要中初始化,因此您不需要以下行:
tableViewChat = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), style: UITableViewStyle.plain)
如果要更改樣式,請在情節提要中進行更改。
而且看起來您正在異步獲取數據:
getUserOnline()
getChatMessage()
這意味着可能需要一些時間才能恢復數據,但您需要立即重新加載表格視圖:
DispatchQueue.main.async {
self.tableViewChat.reloadData()
}
在那個階段,您可能根本沒有數據(因此,表視圖為空)。 您需要一個完成處理程序或委托,該委托處理程序或委托將在數據下載后觸發,然后重新加載表視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.