ho dato un'occhiata a MZTimerLabel
, e viola MVC male. Mette qualcosa che appartiene al modello (il timer che conta giù nel tempo) nella vista. Ecco da dove viene il tuo problema. Le viste dovrebbero poter essere ricreate senza effetti collaterali sul modello.
Consiglierei di abbandonare quella classe e crearne una propria. In realtà è abbastanza facile ottenere qualcosa di simile.
- creare una nuova classe che salva un titolo e un endDate
- memorizzare istanze di quella classe nel modello che esegue il vostro tavolo
- Creare uno NSTimer che rinfresca la tableView
- Impostare la vostra le cellule.
Questo è praticamente tutto il codice necessario per un conto alla rovescia di base in una tabella. Perché non memorizza alcun dato nella visualizzazione è possibile scorrere il più vi piace:
@interface Timer : NSObject
@property (strong, nonatomic) NSDate *endDate;
@property (strong, nonatomic) NSString *title;
@end
@implementation Timer
@end
@interface MasterViewController() {
NSArray *_objects;
NSTimer *_refreshTimer;
}
@end
@implementation MasterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *modelStore = [NSMutableArray arrayWithCapacity:30];
for (NSInteger i = 0; i < 30; i++) {
Timer *timer = [[Timer alloc] init];
timer.endDate = [NSDate dateWithTimeIntervalSinceNow:i*30];
timer.title = [NSString stringWithFormat:@"Timer %ld seconds", (long)i*30];
[modelStore addObject:timer];
}
_objects = modelStore;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_refreshTimer invalidate]; // timer should not exist, but just in case.
_refreshTimer = [NSTimer timerWithTimeInterval:0.5f target:self selector:@selector(refreshView:) userInfo:nil repeats:YES];
// should fire while scrolling, so we need to add the timer manually:
[[NSRunLoop currentRunLoop] addTimer:_refreshTimer forMode:NSRunLoopCommonModes];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_refreshTimer invalidate];
_refreshTimer = nil;
}
- (void)refreshView:(NSTimer *)timer {
// only refresh visible cells
for (UITableViewCell *cell in [self.tableView visibleCells]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self configureCell:cell forRowAtIndexPath:indexPath];
}
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Timer *timer = _objects[indexPath.row];
cell.textLabel.text = timer.title;
NSInteger timeUntilEnd = (NSInteger)[timer.endDate timeIntervalSinceDate:[NSDate date]];
if (timeUntilEnd <= 0) {
cell.detailTextLabel.text = @"Finished";
}
else {
NSInteger seconds = timeUntilEnd % 60;
NSInteger minutes = (timeUntilEnd/60) % 60;
NSInteger hours = (timeUntilEnd/3600);
cell.detailTextLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
@end

fonte
2014-04-21 08:11:01
Cosa si vuole raggiungere? Poiché 'tableView: cellForRowAtIndexPath:' sarà chiamato ogni volta che viene visualizzata una cella, non si deve usare un timer. –
Ho un pulsante di aggiornamento nel mio tableviewCell e quando l'utente tocca il pulsante dovrei sostituire il pulsante con un conto alla rovescia. Anche l'elenco a volte deve essere ricaricato perché i dati provengono da un servizio web. –
Se si utilizzano celle personalizzate, è possibile aggiungere un pulsante nella cella e nasconderlo al clic e mostrare un timer. – iBug