2016-07-02 10 views
0

私のテーブルビューには365個のセルがあります(今の年の日数)今、各セルに日付を表​​示する必要があります。 3、4、5、6、...となります。現在の日付を計算して各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 
} 

は、今私は、現在の日付を取得するメソッドを作成しようとしています:

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] 

} 

のみ問題が各セルに今日の日付を表示する機能の上にあります。 currentDateindexPathに渡す必要があると思って、翌日の予定を推測する必要があります。しかし、私はそうする方法がわかりません!あなたが私を助けてくれたら、私は感謝しています。

答えて

1

基本的には365が今日からスタートされる、次のindexPath.row日一日を追加したい(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!ありがとうございました。これについてはどうすればよいのですか?7月2日が年の初日から「234」であると仮定します。たとえば、テーブルビューを234番目のセルにスクロールする必要がありますか?そこから日付を続けますか? –

+0

@ Mc.Lover特定のセルにプログラム的にスクロールする方法を尋ねるなら、目標を達成する 'scrollToRowAtIndexPath:atScrollPosition:animated:'というメソッドがあります。この場合、最初の日付をJan 1stに初期化し、今日の日付には初期化しません。 –

+0

@ Mc.Lover:次のようにテーブルビューを234番目のセルにスクロールできます: '' ' – shripad20

0

作成するので、あなたのコントローラの上部には、このプロパティを追加します。日付フォーマッタはコストのかかる操作です。表datesource方法において

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

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 

}

関連する問題