2014-08-30 94 views
0

我創建了一個小應用程序,它具有一個UISegmentedControl與段和一個UITableView。當所選段更改時,TableView中的數據(從服務器下載)應該會更改。因此,我有一個方法NSInvocation&NSTimer - 方法被調用兩次

- (void)loadPlanForIndex:(int)tableIndex 
{ 
    // Create PlanModel and get elements 
    _planModel =[[PlanModel alloc] init]; 
    _plan = [_planModel getPlanForDayIndex:tableIndex]; 

    // Reload TableView data 
    [self.firstTable reloadData]; 

    // Set SegmentedControl title 
    NSString *segmentTitle = @„MyTitle「; 
    [self.daySegmentedControl setTitle:segmentTitle forSegmentAtIndex:tableIndex]; 

    // Create NSInvocation to call method with parameters 
    NSInteger objIndex = tableIndex; 
    SEL selector = @selector(loadPlanForIndex:); 
    NSMethodSignature *signature = [self methodSignatureForSelector:selector]; 
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 
    [invocation setTarget:self]; 
    [invocation setSelector:selector]; 
    [invocation setArgument:&objIndex atIndex:2]; 

    NSTimeInterval timeInterval = 10; 
    [NSTimer scheduledTimerWithTimeInterval:timeInterval invocation:invocation repeats:NO]; 
} 

tableIndex是一個索引來獲取正確的數據。 前兩行獲取tableView的數據。

此方法被調用每一個段發生變化時

- (IBAction)didSelectSegment:(id)sender 
{ 
    if (self.daySegmentedControl.selectedSegmentIndex == 0) 
    { 
     [self loadPlanForIndex:0]; 
    } 

    else 
    { 
     [self loadPlanForIndex:1]; 
    } 
} 

這是我的viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // Some other setup 

    [self loadPlanForIndex:1]; 
    [self loadPlanForIndex:0]; 
} 

所以,現在這裏是我的問題:時間用完,並再次調用該方法時,都會我的viewDidLoad的兩個語句被調用(我用NSLog語句檢查過)。因此,在應用程序中顯示索引1的數據,並且只有在顯示正確的數據(用於索引0)後

我該如何解決這個問題?

回答

1

您應該移出調度邏輯關loadPlanForIndex:方法

- (void)loadPlanForIndex:(int)tableIndex 
{ 
    // Create PlanModel and get elements 
    _planModel =[[PlanModel alloc] init]; 
    _plan = [_planModel getPlanForDayIndex:tableIndex]; 

    // Reload TableView data 
    [self.firstTable reloadData]; 

- (void) scheduleAutoReload 
    // Set SegmentedControl title 
    NSString *segmentTitle = @„MyTitle「; 
    [self.daySegmentedControl setTitle:segmentTitle forSegmentAtIndex:tableIndex]; 

    // Create NSInvocation to call method with parameters 
    NSInteger objIndex = tableIndex; 
    SEL selector = @selector(loadPlanForIndex:); 
    NSMethodSignature *signature = [self methodSignatureForSelector:selector]; 
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 
    [invocation setTarget:self]; 
    [invocation setSelector:selector]; 
    [invocation setArgument:&objIndex atIndex:2]; 

    NSTimeInterval timeInterval = 10; 
    [NSTimer scheduledTimerWithTimeInterval:timeInterval invocation:invocation repeats:YES]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self loadPlanForIndex:1]; 
    [self scheduleAutoReload]; 
} 

注意到NSTimerrepeats現在。我不能運行這個,但我認爲它應該工作。無論如何,你可能想把這個定時器保存在一個變量中,所以你可以在viewDidDisappear或類似的東西中停止它。

+0

很酷,謝謝你的工作!你也知道爲什麼它做了這些奇怪的事情嗎? – Codey 2014-08-30 15:45:20

+0

因爲你的方法不斷調用自己,並且再次調用它只是開始新的循環而不停止前面的循環。 – tia 2014-08-30 16:07:42

+0

好的,謝謝! – Codey 2014-08-30 16:21:24