如何根据时间显示 tableviewcells?考虑根据现在的时间显示当前哪些商店正在营业。
有人建议我拉实时,并有条件地 NSlog 结果。那么如何使用大约 30 个单元格(行)有条件地记录 tabview 单元格。
我在objective-c 上很新,所以如果你在你的回复中提供更多细节,那就太好了。
如何根据时间显示 tableviewcells?考虑根据现在的时间显示当前哪些商店正在营业。
有人建议我拉实时,并有条件地 NSlog 结果。那么如何使用大约 30 个单元格(行)有条件地记录 tabview 单元格。
我在objective-c 上很新,所以如果你在你的回复中提供更多细节,那就太好了。
您需要将数据放在数据源中,即 NSMutableArray 中,并且您需要应用 NSPredicate 来根据当前时间过滤数组的数据。您需要以一定的间隔过滤您的主数组,然后重新加载您的表格视图。
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