0

How can I show tableviewcells based on time? Think of showing what current stores are open based on what time it is.

It was suggested to me to pull real time, and NSlog the results conditionally. So how do I log tabview cells conditionally with about 30 cells(rows).

I'm pretty new at objective-c so if you put more detail in your responses that would be great.

  • At timed intervals do your queries or whatever and update the dataSource. If the dataSource is actually changed, do `reloadData`. – Hot Licks May 11 '15 at 00:43

2 Answers2

0

You need to place your data in data source i.e. in NSMutableArray and you need to apply NSPredicate for filtering data of array based on current time. You need to filter your main array at certain interval and then reload you tableview.

Ruchish Shah
  • 319
  • 1
  • 6
0
Suppose, you data structure is like this:

#import <Foundation/Foundation.h>

@interface Store : NSObject

@property (nonatomic, strong) NSString* StoreID;
@property (nonatomic, strong) NSString* StoreName;
@property (nonatomic, strong) NSString* StoreOpenTime;
@property (nonatomic, strong) NSString* StoreCloseTime;

+(BOOL)isBetweenCurrentTime:(NSString*)currentTime;
@end

#import "Store.h"

@implementation Store

+(BOOL)isBetweenCurrentTime:(NSString*)currentTime{
  //Write code which will check whether current time is between opening and closing time of store.
   return YES;
}

@end


#import "MyStoreVC.h"
#import "Store.h"
//This will be your tableviewcell
#import "StoreCell.h" 

@interface MyStoreVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain) IBOutlet UITableView *tbl;
@property (nonatomic, strong) NSMutableArray *StoreList;
@property (nonatomic, strong) NSMutableArray *StoreFilteredList;

@end

@implementation MyStoreVC

- (void)viewDidLoad
{
    [super viewDidLoad];

//this will contain your store information - Fetch this array from server
    self.StoreList = [NSMutableArray new];

//this array will be refreshed at certain interval where you will apply criteria
    self.StoreFilteredList = [NSMutableArray new];

}

-(void)startTimer
{
  //write code to starttimer and it will call filterMeForTime method at regular interval, which will filter your data source and will refresh tableview.

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.StoreFilteredList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Store *s=[self.StoreFilteredList objectAtIndex:indexPath.row];

    StoreCell *cell = [tableview dequeueReusableCellWithIdentifier:@"StoreCell"];
   cell.label.text = s.Title;

    //Write code to create cell and assign data from store
    //no need to write any code to hide unhide cell as your array will contain filtered data

    return cell;  
}

-(void)filterMeForTime:(NSString*)time
{
 NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(Store* evaluatedObject, NSDictionary *bindings) {

        //this method is written in model
        if([evaluatedObject isBetweenCurrentTime:time])
        {
            return true;
        }
        return false;
    }];

    self.StoreFilteredList = [[self.StoreList filteredArrayUsingPredicate:pred] mutableCopy];

   [self.tbl reloadData];
}
@end
Ruchish Shah
  • 319
  • 1
  • 6