2014-09-23 64 views
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 
} 
+0

你的“更新”func被调用? – derdida 2014-09-23 13:33:21

+0

@derdida是的,它确实被调用。 “更新”func中唯一不起作用的部分是self.tableView.reloadData()部分 – 2014-09-23 13:48:28

+0

尝试在主线程上运行它:dispatch_async(dispatch_get_main_queue(),{ } prepMgr.tableView.reloadData() } ) – derdida 2014-09-23 13:59:56

回答

1

你也可以使用NSNotifi以处理表格的“重新加载功能”。如果你需要更新你的桌子,只需打电话给他们。

相关问题