隐藏的 UITableview 部分中 UILabel 中的倒数计时器

Countdown timer in UILabel in hidden UITableview section

我在每个部分的 header 中都有一个 UIView 作为子视图。该视图包含开始按钮和带有倒数计时器的标签。 因此,当计时器启动并且 Tableview 滚动时,某些部分变得隐藏而不是再次显示,它会重新创建我的视图并重置计时器。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

NSString *arrowName = [arrows objectAtIndex:section];

Workouts *workout = [workoutsArray objectAtIndex:section];

UIView* header;
if (section == 0) {
    header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 76)];
} else header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 68)];
header.backgroundColor = [UIColor colorWithRed:0.133f green:0.125f blue:0.247f alpha:1.00f];

//The View with Timer Label
TodayWorkoutView* headerView = [[TodayWorkoutView alloc] initWithFrame:CGRectMake(16, header.frame.size.height-60, header.frame.size.width-32, 60)];
headerView.workout = workout;

[header addSubview:headerView];

return header;

}

如何在每个部分制作计时器,以便在 TableView 滚动时起作用?

我找到了一种解决方案。

在 viewDidLoad 方法中初始化所有视图并添加到新数组:

for (WeekDaysWorkouts *weekWorkout in [self getWokrouts]){

    Workouts *fullWorkout = weekWorkout.workouts;

    [workoutsArray addObject:fullWorkout];

    TodayWorkoutView* headerView = [[TodayWorkoutView alloc] init];
    headerView.workout = fullWorkout;

    [headerViewsArray addObject:headerView];
}

然后我将视图添加到剖面视图:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

NSString *arrowName = [arrows objectAtIndex:section];

UIView* header;
if (section == 0) {
    header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 76)];
} else header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 68)];
header.backgroundColor = [UIColor colorWithRed:0.133f green:0.125f blue:0.247f alpha:1.00f];

//The View with Timer Label
TodayWorkoutView* headerView = [headerViewsArray objectAtIndex:section];

headerView.frame = CGRectMake(16, header.frame.size.height-60, header.frame.size.width-32, 60);

[header addSubview:headerView];

return header;}

因此,所有计时器都不会中断地工作,即使在滚动时也是如此。

如果有人知道更好的解决方案,请告诉我。