iOS Обновление строк UITablieView в зависимости от времени

Как я могу показать табличные ячейки по времени? Подумайте о том, чтобы показать, какие текущие магазины открыты в зависимости от времени.

Мне было предложено вытащить в режиме реального времени, а результаты NSlog условно. Итак, как я могу регистрировать ячейки табуляции условно с примерно 30 ячейками (строками).

Я довольно новичок в цели-c, так что, если вы добавите больше подробностей в свои ответы, это будет здорово.

2 ответа

Вам нужно поместить ваши данные в источник данных, т.е. в 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
Другие вопросы по тегам