ios - 使用 UIPIckerView 自定义 DatePicker

标签 ios objective-c uipickerview

我是 ios 开发的新手。我正在使用 uipickerview 制作自定义日期选择器。我有 datesArray 用作 uipickerview 的数据源。

我想知道如何只显示标签:今天、明天、星期五、星期六、星期日、星期一、星期二和格式为“EEE, LLL d”的休息日期。

我试过这段代码,但没有用。

for(int i=0;i<22;i++)
{
    NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * i];
    NSDate *now=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateFormat:@"EEE, LLL d"];
    if(myDate==now)
    {
        NSString *myDateString=@"today";
         [datesArray addObject:myDateString];
    }
    else
    {
    NSString *myDateString = [dateFormatter stringFromDate:myDate];

    [datesArray addObject:myDateString];
    }

}

最佳答案

试试这个,希望对你有帮助

 NSString *dateFromWS = @"2013-10-16";//im taking it as static you have to take string coming from webservice

 for (int i = 0; i < 22; i++)
 {
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
    NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * i];
    NSDate *now = [dateFormatter1 dateFromString:dateFromWS];
    NSDate *tomorrow = [NSDate dateWithTimeInterval:60 * 60 * 24 * 1 sinceDate:now];
    //NSDate *dummy = [NSDate dateWithTimeInterval:60 * 60 * 24 * 1 sinceDate:now];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateFormat:@"EEE, LLL d"];
    NSString *loopDate = [dateFormatter stringFromDate:myDate];
    NSString *today = [dateFormatter stringFromDate:now];
    NSString *tomorrowString = [dateFormatter stringFromDate:tomorrow];
    if ([loopDate isEqualToString:today]) {
        [datesArray addObject:@"today"];
    } else if ([loopDate isEqualToString:tomorrowString]) {
       [datesArray addObject:@"tomorrow"]; 
    } else if ((i/7) < 1) {
        [datesArray addObject:[loopDate substringToIndex:3]];
    } else {
        [datesArray addObject:loopDate];
    }
 }

关于ios - 使用 UIPIckerView 自定义 DatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19401805/

相关文章:

ios - SSL证书混淆

ios - 在 xcode 上使用 sprite kit 创建黑洞

objective-c - NSError:方法中传递错误

iOS App 导航

arrays - Swift 字典到数组来填充 pickerView

ios - 如何使用 Swift 和 parse.com 在 tableview 中显示图像

objective-c - 将 UIImage 存储为核心数据中的可转换属性

objective-c - 如何在 ARC 下释放带有 block 完成处理程序的对象?

iphone - 重新创建仅显示一行的 UIPickerView

ios - UIPicker selectRow 不工作-这段代码有什么问题?