iOS根据时间更新UITablieView行

时间:2015-05-10 22:48:39

标签: ios objective-c uitableview real-time-clock

如何根据时间显示tableviewcells?考虑根据时间显示当前商店的开放时间。

有人建议我拉实时,并有条件地将结果纳入NSlog。那么如何使用大约30个单元格(行)有条件地记录tabview单元格。

我在objective-c上很新,所以如果你在答案中加入更多细节会很棒。

2 个答案:

答案 0 :(得分:0)

您需要将数据放在数据源中,即在NSMutableArray中,您需要应用NSPredicate来根据当前时间过滤数组的数据。您需要以特定间隔过滤主阵列,然后重新加载tableview。

答案 1 :(得分: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