2016-03-25 5 views
1

Я хочу, чтобы развернуть и свернуть UITableViewCell при использовании UITableViewCell. Моя ячейка, содержащая только 2 UILabel. И я обновляю одно значение UILabel каждые 1 сек (отображение значения таймера обратного отсчета на моем UILabel, поэтому я обновляю значение UILabel каждые 1 сек). Непрерывно NSTimer выстрелил на 1 сек, чтобы произошло мерцание. Пожалуйста, дайте мне решение, если оно известно.Развернуть и свернуть анимацию, мерцающую проблему в UITableview при перезагрузке UITableViewCell каждые 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

Вы перегрузочные клетки после каждых 1 сек, а я хотел бы предложить вам доступ ярлык в timerChangedInRecipe и обновить его текст в основном потоке, если он не обновляется корректно. –

+0

Спасибо за предложение @Bharat Modi –

ответ

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; 
    } 
} 
+1

Вместо того, чтобы перебирать все подвидные объекты, обращайтесь к определенной метке с помощью тега. Как UILabel * labelRecipeDesc = (UILabel) [cell viewWithTag: 1]; –

+0

Да, спасибо @BharatModi –