每1秒重新加载UITableViewCell时,在UITableview中展开和折叠动画闪烁问题

时间:2016-03-25 04:36:04

标签: ios uitableview animation nstimer expand

我希望在点击UITableViewCell时动态扩展和折叠UITableViewCell。 我的细胞只含有2个UILabel。我每1秒更新一个UILabel值(我的UILabel上的倒计时器&#39值显示,这就是为什么我每1秒更新一次UILabel值)。连续NSTimer在1秒后发射,以便发生闪烁。 如果有人知道,请给我解决方案。

提前致谢

我正在使用以下代码

- (void)startTimer
{
    if(_timer == nil)
        _timer =  [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(currentTimerString) userInfo:nil repeats:YES];
    else
        [_timer fire];
}

- (void)stopTimer
{
    if(_timer)
        [self.timer invalidate];
    self.timer = nil;
}

- (void)currentTimerString
{
    self.secondsLeft -- ;
    if(self.secondsLeft > 0 )
    {
        _hours = (int)self.secondsLeft / 3600;
        _minutes = ((int)self.secondsLeft % 3600) / 60;
        _seconds = ((int)self.secondsLeft %3600) % 60;
        self.countTimer = [NSString stringWithFormat:@"%02d:%02d:%02d", self.hours, self.minutes, self.seconds];
        NSLog(@"self.countTimer:%@",self.countTimer);
        if([self.recipeTimerdelegate respondsToSelector:@selector(timerChangedInRecipe:)])
            [self.recipeTimerdelegate timerChangedInRecipe:self];
    }
}
- (void)timerChangedInRecipe:(RecipeTimer *)recipetimer
{
    NSInteger index = recipetimer.recipeTimerId;//recipe.recipeboxId;
    NSIndexPath *rowPath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.timerWindowTbl reloadRowsAtIndexPaths:@[rowPath] withRowAnimation:UITableViewRowAnimationNone];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row_height;
    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {

        row_height=expand_height;// Expanded height
    }
    else
    {
        row_height=collaps_height;
    }
    return row_height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self expandCell:indexPath];
}


#pragma mark - Expand Cell
- (void)expandCell:(NSIndexPath *)indexPath
{
    // self.expandedIndexPath=indexPath;
    [self.tableView beginUpdates];

    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame)
    {
        self.expandedIndexPath = nil;
        [self.tableView endUpdates];

    }
    else
    {
        self.expandedIndexPath = indexPath;
        [self.tableView endUpdates];

        [UIView animateWithDuration:0.7
                              delay:0.0
             usingSpringWithDamping:1.0
              initialSpringVelocity:4.0
                            options: UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             if([indexPath row]==((NSIndexPath*)[[self.tableView indexPathsForVisibleRows]lastObject]).row)
                             {
                                 [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.expandedIndexPath.row inSection:self.expandedIndexPath.section] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                             }

                         }
                         completion:^(BOOL finished){

                         }];
        [UIView commitAnimations];
    }

}

1 个答案:

答案 0 :(得分:0)

- (void)timerChangedInRecipe:(RecipeTimer *)recipetimer
{
    NSInteger index = recipetimer.recipeTimerId;//recipe.recipeboxId;
    NSIndexPath *rowPath = [NSIndexPath indexPathForRow:index inSection:0];
//    [self.timerWindowTbl reloadRowsAtIndexPaths:@[rowPath] withRowAnimation:UITableViewRowAnimationNone];

    UITableViewCell *cell = [self.timerWindowTbl cellForRowAtIndexPath:rowPath];
        for(UILabel *lbl in [cell.contentView subviews])
            {
                if([lbl isKindOfClass:[UILabel class]])
                {
                    if(lbl.tag == 1)
                    {
                        lbl.text=recipetimer.recipeDesc;
                    }
                    if(lbl.tag == 2)
                    {
                        lbl.text=recipetimer.countTimer;
                    }
                }

    }
 }

  - (void)timerChangedInRecipe:(RecipeTimer *)recipetimer
        {
            NSInteger index = recipetimer.recipeTimerId;//recipe.recipeboxId;
            NSIndexPath *rowPath = [NSIndexPath indexPathForRow:index inSection:0];
        //    [self.timerWindowTbl reloadRowsAtIndexPaths:@[rowPath] withRowAnimation:UITableViewRowAnimationNone];

            UITableViewCell *cell = [self.timerWindowTbl cellForRowAtIndexPath:rowPath];
            UILabel *labelRecipeDesc = (UILabel*) [cell viewWithTag: 1];
            labelRecipeDesc.text=recipetimer.recipeDesc;

            UILabel *labelRecipeTimerCounter = (UILabel*) [cell viewWithTag: 2];
            labelRecipeTimerCounter.text=recipetimer.countTimer;
    }
}