small medium large xlarge

Img_7226_pragsmall
21 Nov 2016, 11:16
memetcircus (5 posts)

Hi! I tried to modify table with the code below. But it couldn’t add new row to the table. If I put a breakpoint under if part, it couldn’t pass to the insert part as the context is nil. We set Run values in init part but it seems somehow a func should be triggered in init part to ensure context includes Run values

import WatchKit
import Foundation
 class RunLogInterfaceController: WKInterfaceController {

 @IBOutlet var runTable: WKInterfaceTable!

 var runs:[Run]?

 lazy var dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    return dateFormatter
 }()
    
lazy var distanceFormatter = LengthFormatter()

lazy var durationFormatter: DateComponentsFormatter = {
   let dateComponentsFormatter = DateComponentsFormatter()
    dateComponentsFormatter.unitsStyle = .positional
    return dateComponentsFormatter
}()

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    
    if let run = context as? Run {
        
        runs?.insert(run, at: 0)
        
        runTable.insertRows(at: NSIndexSet(index:0) as IndexSet, withRowType: "RunRow")
        
        if let rowc = runTable.rowController(at: 0) as? RunLogRowController{
            configureRow(rowController: rowc, forRun: run)
        }
    }
}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
    
    guard let runs = runs else { return }
    
    runTable.setNumberOfRows(runs.count, withRowType: "RunRow")
    
    for i in 0 ..< runTable.numberOfRows {
        guard let rowController = runTable.rowController(at: i) as? RunLogRowController else {continue}
        
        configureRow(rowController: rowController, forRun: runs[i])
        
    }

}

func configureRow(rowController: RunLogRowController, forRun run:Run){
    
    rowController.dateFormatter = dateFormatter
    rowController.durationFormatter = durationFormatter
    rowController.distanceFormatter = distanceFormatter
    
    rowController.configure(date: run.startDate as Date , distance: run.distance, duration: run.duration)
    
}

override init() {
    srand48(time(UnsafeMutablePointer<time_t>(bitPattern: 0)))
    
    let randomRun: (NSDate) -> Run = { date in
        
        let lapCount = arc4random_uniform(20) + 5
        let lapDistance = arc4random_uniform(1000) + 1
        
        var laps: [TimeInterval] = []
        
        for _ in 0 ..< lapCount {
            let speed = 3.0 + (drand48() - 0.5)
            let lapDuration : TimeInterval = Double(lapDistance) / speed
            laps.append(lapDuration)
        }
        
        let run = Run(distance: Double(lapDistance * lapCount), laps: laps, startDate: date as Date)
        
        return run
        
    }
    
    runs = []
    
    for i in 0 ..< 5 {
        runs?.append(randomRun(Date().addingTimeInterval(Double(i) * 24 * 60 * 60) as NSDate))
    }
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
} }
You must be logged in to comment