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



我在每个部分的标题中都有一个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 滚动时起作用?

我找到了一个解决方案。

In 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;}

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

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

最新更新