3
我試圖從我的TimersManager.swift文件中重新加載我的UITableView時遇到了問題。 TimersManager.swift用於控制/管理待辦事項列表/計時器應用程序中的所有計時器。我正在嘗試更新UILabel以在計時器結束時顯示更新的時間。出於某種原因,它不會更新表格。請在下面看看,希望你能給我一個正確的方向。謝謝。SWIFT - 重新加載UITableViewCell
頂部listTableViewController.swift的:在listTableViewController
var prepMgr: listTableViewController = listTableViewController()
var cell:customCell!
class listTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
更新FUNC:
func update (indexPathRow: Int) {
for task in taskMgr.tasks {
if task.timerOn == true {
//calculate the time to display in 0:00:00 format.
let date1 : NSDate = task.timerFinishDate
let date2 : NSDate = NSDate()
let compareResult = date1.compare(date2)
let length = Int(round(date1.timeIntervalSinceDate(date2)))
var tmpHours = length/3600
var tmpMinutes = (length % 3600)/60
var tmpSeconds = length % 60
var timeString = "\(tmpHours):\(tmpMinutes):\(tmpSeconds)"
println(task.subText) //test, display old value before update - WORKS
taskMgr.updateTask(indexPathRow, name: taskMgr.tasks[indexPathRow].name, subText: timeString, timerOn: taskMgr.tasks[indexPathRow].timerOn, completed: taskMgr.tasks[indexPathRow].completed, timerFinishDate: taskMgr.tasks[indexPathRow].timerFinishDate, taskID: taskMgr.tasks[indexPathRow].taskID, sliderHours: taskMgr.tasks[indexPathRow].sliderHours, sliderMinutes:taskMgr.tasks[indexPathRow].sliderMinutes, sliderSeconds: taskMgr.tasks[indexPathRow].sliderSeconds)
println(task.subText) //test, display updated value after update - WORKS
println(timeString) //test, display time remaining in timer 0:00:00 - WORKS
}
self.tableView.reloadData() // DOES NOT UPDATE TABLE.
}
}
爲的NSTimer選擇的代碼(這是由我的TimersManager.swift文件中的另一個FUNC調用) TimersManager.swift:
func tickTock (length:NSTimer!) {
println(length.userInfo)
var count = 0
for timer in timers {
let date1 : NSDate = timer.fireDate
let date2 : NSDate = NSDate()
let compareResult = date1.compare(date2)
let length = Int(round(date1.timeIntervalSinceDate(date2)))
if length <= 0 {
//Invalidate NSTimer
timer.myTimer.invalidate()
//Remove from array
timers.removeAtIndex(count)
}
count++
println(length) //test, shows how many seconds are left - WORKS
//update labels.
prepMgr.update(timer.indexPathRow) //Call to listTableViewController func - Half working, calls the function. updates the correct task. But table is not reloaded.
}
//update labels, reload table
prepMgr.tableView.reloadData() //Test, Not working
}
你的「更新」func被調用? – derdida 2014-09-23 13:33:21
@derdida是的,它確實被調用。 「更新」func中唯一不起作用的部分是self.tableView.reloadData()部分 – 2014-09-23 13:48:28
嘗試在主線程上運行它:dispatch_async(dispatch_get_main_queue(),{ } prepMgr.tableView.reloadData() } ) – derdida 2014-09-23 13:59:56