[英]UITableViewCell countdown UILabel not updating
我試圖了解有關計時器的更多信息,所以我決定嘗試創建Todo列表類型的IOS應用。 在應用程序中,我希望用戶可以根據需要向每個任務/項目添加計時器。
到目前為止,用戶可以選擇一個時間(從選擇器中選擇),啟動和停止計時器而沒有任何問題。 我遇到的問題是UILabel將不會更新並顯示倒數計時器文本。 它唯一更新的時間是當我轉到一個子視圖(編輯任務),然后返回到主viewController(任務列表)時。
我可以更新單元格的唯一方法是使用[self.tableView reloadData]在父viewController中運行循環; 我很確定這就像用大錘殺死蒼蠅一樣。
任何見識都會很棒。 謝謝。
編輯 :
@property (strong, nonatomic) IBOutlet UILabel *taskNameTextLabel; //Textfield for item
@property (strong, nonatomic) IBOutlet UILabel *taskSubtitleTextLabel; //Textfield to timer
我為tableview單元創建了一個子面板。 這是我在taskTableViewCell.m文件中用於計時器的代碼。
-(void) stopTimer {
[self.timer invalidate];
self.timer = nil;
}
- (void) startTimer
{
[self stopTimer];
NSLog(@"time : %f", _taskCountDownInterval);
[self calculateTimer];
_timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(countDownTimerHandler)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)calculateTimer
{
//calculate total seconds from the saved values the user selected using the picker.
int hours = [self.startHours integerValue];
int minutes = [self.startMinutes integerValue];
int seconds = [self.startSeconds integerValue];
NSLog(@"h: %i m: %i s: %i", hours,minutes,seconds);
//convert to seconds and get total sum
hours = hours *3600;
minutes = minutes *60;
int totalSeconds = hours + minutes + seconds;
_taskCountDownInterval = totalSeconds;
}
- ( void) countDownTimerHandler
{
int remainingTime = _taskCountDownInterval--;
if (remainingTime <=0)
{
[_countDownTimer invalidate];
_countDownTimer = nil;
remainingTime = 0;
NSLog(@"Timer finished!");
}
//convert for text display.
int hours = remainingTime / 3600;
int scratch = remainingTime % 3600;
int minutes = scratch / 60;
int seconds = scratch % 60;
NSString *timeValue = [NSString stringWithFormat:@"%i:%i:%i",hours,minutes,seconds];
self.subtitle = timeValue; //save the converted string.
NSLog(@"label should be %@",timeValue);
NSLog(@"label is %@",self.taskSubtitleTextLabel.text);
//[[self taskSubtitleTextLabel] setText:_subtitle];
_taskSubtitleTextLabel.text = _subtitle;
}
日志輸出為:
2014-06-18 11:52:22.614 mytools2[1438:60b] turn timer ON
2014-06-18 11:52:22.614 mytools2[1438:60b] total time in seconds : 21966.000000
2014-06-18 11:52:22.615 mytools2[1438:60b] h: 6 m: 6 s: 6
2014-06-18 11:52:22.615 mytools2[1438:60b] taskCountDownInterval 21966.000000
2014-06-18 11:52:23.616 mytools2[1438:60b] label should be 6:6:6
2014-06-18 11:52:23.617 mytools2[1438:60b] label is (null)
2014-06-18 11:52:24.616 mytools2[1438:60b] label should be 6:6:5
2014-06-18 11:52:24.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:25.616 mytools2[1438:60b] label should be 6:6:4
2014-06-18 11:52:25.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:26.616 mytools2[1438:60b] label should be 6:6:3
2014-06-18 11:52:26.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:27.616 mytools2[1438:60b] label should be 6:6:2
2014-06-18 11:52:27.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:28.616 mytools2[1438:60b] label should be 6:6:1
2014-06-18 11:52:28.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:29.616 mytools2[1438:60b] label should be 6:6:0
2014-06-18 11:52:29.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:30.615 mytools2[1438:60b] label should be 6:5:59
2014-06-18 11:52:30.616 mytools2[1438:60b] label is (null)
2014-06-18 11:52:30.773 mytools2[1438:60b] turn timer OFF
您可以使用reloadRowsAtIndexPaths:withRowAnimation:
僅更新需要更新的單元格(包含標簽的單元格),而不是更新整個表:
[self.tableView reloadRowsAtIndexPaths:@[yourIndexPath] withRowAnimation: UITableViewRowAnimationNone]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.