简体   繁体   English

过滤json数据并将结果数据重新加载到myTableView中

[英]Filtering json data and reloading the resulted data into myTableView

Im trying to load an events(data) (from json file) that matches today's date into my tableView when the today's button is pressed then only the data with today's date will be loaded there and when the future button is pressed the events with later dates will be loaded, the only problem I have here is when i run my program nothing is actually happening when I press in either of these UIbuttons, I got all the events regardless their date and without even clicking on any UIButton, u can see below some of my code, it would be great if someone could figure out what is the problem there and since Im not quiet sure about how to use predicate to filter the data: 我试图将与今天的日期匹配的事件(数据)(从json文件)加载到我的tableView中,当按下``今天''的按钮时,则仅将具有今天日期的数据加载到那里,而当按下``将来''按钮时,将显示具有较晚日期的事件将被加载,我这里唯一的问题是当我运行程序时,当我按下这两个UIbutton中的任何一个时,实际上什么都没有发生,我获得了所有事件,无论它们的日期如何,甚至都没有单击任何UIButton,您可以在下面看到一些在我的代码中,如果有人可以找出那里的问题,并且因为我不太确定如何使用谓词来过滤数据,那将是很好的:

@property (strong, nonatomic) NSArray *dateEvents;

@end


@implementation HomeViewController {
 NSArray *_events;
 NSArray *_dateEvents;


 }



- (IBAction)upcomingEvents:(id)sender {


NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-YYYY"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(@"mama"); 

_dateEvents = [_events filteredArrayUsingPredicate:[NSPredicate   predicateWithBlock:^BOOL(Events * event, NSDictionary *bindings){

    return [event.date isEqualToString:dateString];
    }]];
         self.myTableView.dataSource = self;


         [self.myTableView reloadData];
         NSLog(@"yes");

  }

Thanks, 谢谢,

I'm not sure this will definitely help, because I can't see enough code so be certain that this is your problem, but you might find it useful to convert all the dates to NSDate objects (so that you can be certain that the problem is NOT that the strings are actually not equal), and then check whether the day is the current day, or not, by using this category method on NSDate: 我不确定这肯定会有所帮助,因为我看不到足够的代码,因此可以确定这是您的问题,但是您可能会发现将所有日期转换为NSDate对象很有用(这样就可以确定问题不是字符串实际上不相等),然后通过在NSDate上使用此类别方法来检查日期是否为当前日期:

NSDate+Extensions.m: NSDate + Extensions.m:

static NSCalendar* ___calendar;

@implementation NSDate (Extensions)

    - (BOOL)isSameDay:(NSDate*)date
{
    NSCalendar* calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:self];

    return [comp1 day]   == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}

-(NSCalendar*)currentCalendar
{
    if(___calendar == nil)
    {
        ___calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    }

    return ___calendar;
}

@end

NSDate+Extensions.h NSDate + Extensions.h

@interface NSDate (Extensions)

- (BOOL)isSameDay:(NSDate*)date;

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM