2014-08-30 20 views
-3

我正在構建一個應用程序,在該應用程序中單擊按鈕並在選定單元格的標籤中運行NSTimer。我正在使用一個原型單元格,並在其中有兩個標籤:名稱和時間。這些標籤分別有自己的標籤:1010和1020。下面的照片是它在運行時的樣子。基本上,我正在尋找的是在選定的單元格中啓動一個計時器。沒有計時器在每個單元格中運行,我似乎無法做到這一點。在特定的UITableViewCell上更改特定的UILabel

重要提示:照片中的原型單元格僅爲示例。在我的項目中,我只使用一(1)個標識符的原型單元格。

感謝您花時間閱讀本文,如果您希望我提供更多信息以幫助您回答問題,我會非常樂意這樣做!

編輯:在運行時,顯示十(10)個單元格。 NSArrays正在填充數據。

編輯2:我一直得到的問題是計時器在每個單元格中。我想弄清楚如何針對特定的細胞。我已經儲存了手機號碼。我該怎麼辦?

int cellNumber; 
bool isCurrentlyRunning = NO; 

- (void)viewDidLoad{ 
[super viewDidLoad]; 
//Table Data Code 
self.peopleArray = @[@"Person 1", @"Person 2", @"Person 3", @"Person 4", @"Person 5", @"Person 6", @"Person 7", @"Person 8", @"Person 9", @"Person 10"]; 
self.TimeArray = @[@"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00"]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.peopleArray count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SimpleIdentifier"]; 

    UILabel *lblName = (UILabel *)[cell viewWithTag:1010]; 
    UILabel *lblTime = (UILabel *)[cell viewWithTag:1020]; 

    NSString *Name, *Time; 

    Name = [self.roundOrder objectAtIndex:indexPath.row]; 

    lblTime.text = [self formattedTime:self.currentTimeInSeconds]; 
    lblName.text = Name; 

    return cell; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    [_myTimer invalidate]; 
    self.currentTimeInSeconds = 0; 
    cellNumber = [indexPath row]; 
} 

- (int)timeToStop{ 
    int retVal = 0; 
    switch (cellNumber) { 
     case 0: 
      retVal = 100; 
     break; 
     default: 
      retVal = 200; 
     break; 
    } 
    return retVal; 
} 
- (NSTimer *)createTimer { 
    return [NSTimer scheduledTimerWithTimeInterval:1.0 
              target:self 
              selector:@selector(timerTicked:) 
              userInfo:nil 
              repeats:YES]; 
} 
- (NSString *)formattedTime:(int)totalSeconds 
{ 

    int seconds = totalSeconds % 60; 
    int minutes = (totalSeconds/60) % 60; 

    return [NSString stringWithFormat:@"%d:%02d", minutes, seconds]; 
} 
- (void)timerTicked:(NSTimer *)timer { 

    _currentTimeInSeconds++; 
    if (self.currentTimeInSeconds == [self timeToStop]){ 
     [_myTimer invalidate]; 
    } 
} 
- (IBAction)startCountDown:(id)sender { 

    if (!_currentTimeInSeconds) { 
     _currentTimeInSeconds = 0 ; 
    } 
    if (!_myTimer) { 
     _myTimer = [self createTimer]; 
    } 
    isCurrentlyRunning = YES; 
} 
- (IBAction)stopCountDown:(id)sender { 

    if(isCurrentlyRunning == YES){ 
     [_myTimer invalidate]; 
     isCurrentlyRunning = NO; 
    }else{ 
     if (_myTimer) { 
     [_myTimer invalidate]; 
    } 
     _currentTimeInSeconds = 0; 
     self.lblTimer.text = [self formattedTime:_currentTimeInSeconds]; 
} 

}

+0

「我似乎無法做到這一點,而無需在每個小區用的定時器運行。」但是你沒有展示你所做的。我的意思是,在抽象中很明顯的是如何做到這一點,但你需要做的是修正你已經在做的事情,以便它定位一個特定的標籤。那麼,爲什麼你不顯示你在做什麼,如果你需要幫助呢? – matt 2014-08-30 03:39:40

+0

嘿@Ares檢查我的答案。,,, – 2014-08-30 07:10:19

+0

嘿@matt!對於那個很抱歉。我添加了必要的代碼。我認爲我的問題起初更多的是一般問題,但現在我看它,事實並非如此。這是特定的,所以我添加了代碼。 – Ares 2014-08-30 15:17:39

回答

-1

您可以使用 - (UITableViewCell的*)的cellForRowAtIndexPath:(NSIndexPath *)indexPath

構造您NSIndexPath與部分0和行要更新。然後你可以訪問每個標籤。

0

您需要使用scheduledTimerWithTimeInterval爲表格中的每一行創建一個NSTimer的數組:target:selector:userInfo:repeatats:並在userInfo參數中提供NSIndexPath。然後,當您選擇器被調用時,您可以使用indexPathsForVisibleRows來檢查索引路徑是否可見,如果可以,可以使用cellForRowAtIndexPath來獲取單元格。然後使用標籤獲取標籤。

-1

你需要看看

-didSelectrowAtIndexPath method. 

這種方法會解僱你根據其索引路徑打了一個原型細胞每次。

如果你的單元格有單獨的標籤,那麼你可以做一個檢查,或者所有的單元格都有類似的內容,你正在使用這個標識符來重用這些單元格,那麼你所要做的就是激發那個單元格的nstimer方法。

經歷的方法一些例子,我希望這有助於

相關問題