我正在構建一個應用程序,在該應用程序中單擊按鈕並在選定單元格的標籤中運行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];
}
}
「我似乎無法做到這一點,而無需在每個小區用的定時器運行。」但是你沒有展示你所做的。我的意思是,在抽象中很明顯的是如何做到這一點,但你需要做的是修正你已經在做的事情,以便它定位一個特定的標籤。那麼,爲什麼你不顯示你在做什麼,如果你需要幫助呢? – matt 2014-08-30 03:39:40
嘿@Ares檢查我的答案。,,, – 2014-08-30 07:10:19
嘿@matt!對於那個很抱歉。我添加了必要的代碼。我認爲我的問題起初更多的是一般問題,但現在我看它,事實並非如此。這是特定的,所以我添加了代碼。 – Ares 2014-08-30 15:17:39