2014-04-21 3 views
5

मैं https://github.com/mineschan/MZTimerLabel/ और मेरे tableview cellForRowAtIndex में उपयोग कर रहा हूँ नीचे की तरह टाइमर का उपयोग करने के बाद घड़ी बनाता है टाइमर अजीब व्यवहार करता है और लगता है कि यह एक ही स्थान पर गिनने के लिए कई टाइमर पुन: उत्पन्न करता है। इस टाइमर का उपयोग करते समय मुझे इसे कैसे ठीक करना चाहिए?का उपयोग करते हुए टाइमर किसी भी पुस्तक या तालिका पुनः लोड

किसी भी मदद की सराहना,

एलियास

+0

आप क्या हासिल करना चाहते हैं? चूंकि 'tableView: cellForRowAtIndexPath: प्रत्येक बार सेल प्रदर्शित होने पर आपको कॉल किया जाएगा, आपको वहां एक टाइमर का उपयोग नहीं करना चाहिए। –

+0

मेरे पास मेरे tableviewCel में एक अपग्रेड बटन है और जब उपयोगकर्ता बटन पर टैप करता है तो मुझे बटन को गिनती टाइमर के साथ बदलना चाहिए। इसके अलावा सूची को कभी-कभी पुनः लोड करने की आवश्यकता होती है क्योंकि डेटा किसी webservice से आ रहा है। –

+0

यदि आप कस्टम सेल का उपयोग कर रहे हैं, तो आप सेल में एक बटन जोड़ सकते हैं और इसे क्लिक करके छुपा सकते हैं और टाइमर दिखा सकते हैं। – iBug

उत्तर

8

मैं MZTimerLabel पर एक नज़र था, और यह MVC बुरी तरह उल्लंघन करती है। यह उस चीज को रखता है जो मॉडल में होता है (टाइमर जो समय के नीचे गिना जाता है) दृश्य में होता है। यही वह जगह है जहां से आपकी समस्या आती है। मॉडल पर साइड इफेक्ट किए बिना दृश्यों को फिर से बनाया जा सकता है।

मैं उस वर्ग को कुचलने की सिफारिश करता हूं, और अपना खुद का निर्माण करता हूं। यह वास्तव में कुछ हासिल करने के लिए काफी आसान है।

  1. एक नया वर्ग है कि एक शीर्षक और एक ENDDATE मॉडल में उस वर्ग कि अपनी मेज पीठ की
  2. स्टोर उदाहरणों बचाता बनाएं
  3. एक NSTimer कि tableView
  4. सेट करें ताज़ा करता है अपना कोशिकाओं।

यह मूल रूप से एक तालिका में मूल उलटी गिनती के लिए आवश्यक सभी कोड है। क्योंकि यह ध्यान में रखते हुए किसी भी डेटा की दुकान नहीं है आप के रूप में ज्यादा आप की तरह के रूप में स्क्रॉल कर सकते हैं:

@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 

enter image description here

+0

आपको बहुत बहुत धन्यवाद। मेरी कक्षाएं इतनी बड़ी और जटिल हैं, आपके समाधान को आजमाने में थोड़ा समय लगता है। मैं इसका उपयोग शुरू कर रहा हूं और खत्म करने के बाद आपको जवाब स्वीकार करूंगा। धन्यवाद। –

+0

बहुत बढ़िया! पूरी तरह से काम किया। बहुत बहुत धन्यवाद। –

+0

हाय मैथियस, मुझे अपने पिछले लिंक के माध्यम से यहां आपके प्रश्न पर रीडायरेक्ट किया गया था: http://stackoverflow.com/questions/29961244/set-uitableviewcell-contents-only-once। मुझे आपका टाइमर काम करने के लिए मिला और यह अच्छी तरह से काम करता है। हालांकि, जब मैं 'UITableView' को ऊपर और नीचे स्क्रॉल करता हूं तो केवल तभी ताज़ा होता है। क्या वास्तविक समय में उन्हें ताज़ा करने का कोई तरीका है? मतलब यह है कि उपयोगकर्ता हर सेकंड नीचे जाने के समय देख सकते हैं। यदि आप मेरी मदद कर सकते हैं, तो यह बहुत अच्छा होगा। धन्यवाद! – user1871869

संबंधित मुद्दे