每 1 秒重新加载 UITableViewCell 时,UITableview 中的展开和折叠动画闪烁问题
Expand and Collapse animation flickering issue in UITableview when reload UITableViewCell on every 1 sec
我想在点击 UITableViewCell 时动画地展开和折叠 UITableViewCell。
我的单元格只包含 2 个 UILabel。我每 1 秒更新一次 UILabel 值(倒数计时器的值显示在我的 UILabel 上,这就是为什么我每 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];
}
}
- (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;
}
}
我想在点击 UITableViewCell 时动画地展开和折叠 UITableViewCell。 我的单元格只包含 2 个 UILabel。我每 1 秒更新一次 UILabel 值(倒数计时器的值显示在我的 UILabel 上,这就是为什么我每 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];
}
}
- (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;
}
}