2016-07-02 1 views
0

meine Tabellenansicht hat 365 Zellen (Anzahl der Tage im Jahr) jetzt muss ich jedes Datum in jeder einzelnen Zelle anzeigen wenn zB heute 2 Jul, die nächste Zelle sollte sei 3 dann 4, 5, 6 und ....Wie berechnet man das aktuelle Datum und zeigt es jedem UITableViewCell

 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

     return 1 
    } 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return 365 
    } 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // ... setup custom cell 

    cell.day.text = dayIndexPath() 
    return cell 
} 

jetzt versuche ich, ein Verfahren zu schaffen, das aktuelle Datum wird:

func dayIndexPath(indexPath:NSIndexPath) -> String { 

     let currentDate = NSDate() 
     let dateFormatter = NSDateFormatter() 
     dateFormatter.locale = NSLocale.currentLocale() 

     var convertedDate = dateFormatter.stringFromDate(currentDate) 
     dateFormatter.dateFormat = "d" 
     convertedDate = dateFormatter.stringFromDate(currentDate) 

     let dayArray = [convertedDate] 

     let row:Int = indexPath.row 
     return dayArray[row % dayArray.count] 

} 

Das Problem obige Funktion heute nur Datum ist in jeder Zelle zeigt. Ich denke, ich muss currentDate zu indexPath übergeben und dann berechnen, was der nächste Tag ist. Aber ich bin mir nicht sicher, wie ich das machen soll! Ich wäre Ihnen dankbar, wenn Sie mir helfen würden.

Antwort

1

Grundsätzlich Sie einen Tag für den nächsten indexPath.row Tage hinzufügen möchten, die 365 ab heute sein wird (indexPath.row = 0)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 

    let todayDate = NSDate() 
    let dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "d" 
    let calculatedDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.Day, value: indexPath.row, toDate: todayDate, options: NSCalendarOptions.init(rawValue: 0)) 
    let calculatedDayString = dateFormatter.stringFromDate(calculatedDate!) 

    cell.textLabel?.text = calculatedDayString 
    return cell 
} 
+0

Greaaaat! Vielen Dank. Was ist damit: Nehmen wir an, 2 Juli ist '234 'vom ersten Tag des Jahres, wie soll ich die Tabellenansicht zum Beispiel auf 234. Zelle scrollen? und weiter von da date? –

+0

@ Mc.Lover Wenn Sie fragen, wie Sie zu einer bestimmten Zelle programmgesteuert scrollen können, gibt es eine Methode mit dem Namen 'scrollToRowAtIndexPath: atScrollPosition: animated:', die das Ziel erreicht. In diesem Fall möchten Sie das erste Datum auf den 1. Januar und nicht auf das heutige Datum initialisieren. –

+0

@ Mc.Lover: Sie können Tabellenansicht zu 234. Zelle wie folgt scrollen: 'Lassen Sie IndexPath = NSIndexPath (forRow: 234, inSection: 0)' 'tableView.scrollToRowAtIndexPath (indexPath, atScrollPosition: .Top, animiert: True)' – shripad20

0

Fügen Sie diese Eigenschaft an der Spitze des Controllers, weil Schöpfung des Datums Formatierer ist kostspieliger Betrieb.

lazy var dateFormatter: NSDateFormatter = { 
    let dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "d" 
    return dateFormatter 
    }() 

In Tabelle datesource Methode

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

let todayDate = NSDate() 
let finalDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.Day, value: indexPath.row, toDate: todayDate, options: NSCalendarOptions.init(rawValue: 0)) 
if let finalDate = finalDate { 
    cell.textLabel?.text = dateFormatter.stringFromDate(finalDate) 
} 

return cell 

}

Verwandte Themen