我正在創建一個需要將一年中的所有日期放入一個uitableview(每個日期都有自己的單元格)的應用程序。有沒有辦法使用nsdate來填充每個日期的每個單元格?將所有日曆日期插入到一個可用視圖中。
3
A
回答
0
如果需要顯示從1月1日到12月31日的所有日期,則需要手動創建NSDate
實例。
- (NSInteger)daysInYear:(NSInteger)year {
if (year % 400 == 0) return 366;
else if (year % 100 == 0) return 365;
else if (year % 4 == 0) return 366;
else return 365;
}
- (NSDate *)firstDayInYear:(NSInteger)year {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"MM/dd/yyyy"];
NSDate *firstDayInYear = [fmt dateFromString:[NSString stringWithFormat:@"01/01/%d", year]];
[fmt release];
return firstDayInYear;
}
- (NSDate *)dateWithDayInterval:(NSInteger)dayInterval sinceDate:(NSDate *)referenceDate {
static NSInteger SECONDS_PER_DAY = 60 * 60 * 24;
return [NSDate dateWithTimeInterval:dayInterval * SECONDS_PER_DAY sinceDate:referenceDate];
}
在你的tableView:的cellForRowAtIndexPath:方法,你就可以實例化相應的NSDate
實例:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDate *dateForCell = [self dateWithDayInterval:indexPath.row sinceDate:self.firstDayInYear];
// ... rest of your method ...
}
相關問題
- 1. 將日期插入到兩個表中
- 2. 將選定的日曆日期放入視圖
- 3. 使用日曆表填寫另一個日期範圍內的所有日期
- 4. 將日期插入到SQLite
- 5. 將日期插入到MySQL
- 6. 如何在2015年日期字段中插入所有日期?
- 7. 將所有剩餘日期直到特定日期插入滑塊
- 8. 將日期插入一個範圍
- 9. 使用日曆表,某個日期範圍內插入值
- 10. 在一個時間段內插入表中的所有日期
- 11. 輸入一個開始日期和結束日期,並將其中的日期插入到db
- 12. 是否可以將兩個日期值插入到SimpleDateFormat中?
- 13. 從excel導入日期到Outlook日曆
- 14. 無法將日期插入到HSQLdb中
- 15. 將日期插入到Oracle中
- 16. 將日期插入到db2中
- 17. Hadoop可寫日期/日曆
- 18. WPF日曆可見日期
- 19. 插入日期到日期9
- 20. 將活動從一個日曆複製到另一個日曆
- 21. 左加入日期所有日期
- 22. 從Google日曆導入時,D日曆日期有錯誤的日期?
- 23. C#ASP.NET - 從日曆插入日期到數據庫
- 24. UWP日曆視圖可見日期範圍
- 25. Php將日曆月添加到日期
- 26. ColdFusion - 日期/時間 - 插入日期範圍的可用插槽
- 27. Tapku日曆在選擇日期後推送到另一個視圖
- 28. 禁用除日曆中的特定日期以外的所有日期wpf
- 29. 如何將一個日期作爲參數插入到mysql中?
- 30. 將SharePoint日曆視圖連接到Outlook
的
以下方法可能與您需要爲您
UITableView
細胞NSDate
實例幫助這個問題的答案看起來很有用:http://stackoverflow.com/questions/1472386/number-of-days-in-given-year-using-iphone-sdk – jtbandes