繁体   English   中英

UITableViewCell内部的动画UILabel文本更改

[英]Animate UILabel text change inside UITableViewCell

当文本属性更改时,我想为自定义UITableViewCellUILabel设置动画。

我的动画效果很好,除了动画在每次滚动和调用[myTableView reloadData]时每次进入单元格时都会触发。

使用简单的东西

if (![myLabel.text isEqualToString:newText]) {
    // change myLabel.text with animation here. 
}

不起作用。

我了解为什么会这样。 如果我每次都创建一个新单元格,则可以检查myLabel.text是否包含字符串,如果有,是否不同,然后应用更改。 但是,有时会有太多的单元格无法显示。

当重用单元格时, myLabel.text几乎总是已经有一个字符串,并且几乎总是与我要设置的字符串不同。 因此上述检查无效。 无论如何,它们将导致动画触发。

我想让它起作用的唯一方法是使用myLabel.text的最后一个已知值创建一个数组并进行比较,但是此列表非常庞大!

我不知道从哪里开始解决这个问题。

我想要的是myLabeltext属性更改时在我的子类UITableViewCell做一些flip / backgroundColor动画。 我找到的最接近的解决方案将导致tableviewcell中的所有标签运行动画。

在给我一个字符串之前,我也无法检查myLabel.text ,因为它总是被重用。

我开始觉得这是做不到的。

如果已将UITableViewCell子类化,则可以覆盖默认的prepareForReuse方法。 在此方法中,将标签的text属性设置为nil

- (void)prepareForReuse {
    [super prepareForReuse];
    myLabel.text = nil;
}

现在:

if (myLabel.text && ![myLabel.text isEqualToString:newText]) {
    // change myLabel.text with animation here. 
}

现在,仅当将文本从一个文本更改为新文本时,该文本才应设置动画。

prepareForReuse dequeueReusableCelldequeueReusableCell方法在单元格上调用prepareForReuse

#pragma mark - Timer Function
-(void)callAfteroneSecond_deals
{
    NSArray *indexes = [self.tbl_deals indexPathsForVisibleRows];
    MYTableViewCell *cell = (MYTableViewCell *)[_tbl_deals cellForRowAtIndexPath:index];


    cell.lbl_expiredtime.text=@"Hello";
    [cell.contentView bringSubviewToFront:cell.lbl_expiredtime];
}

我将在自定义单元格上定义一个方法,如下所示:

   - (void) updateMyLabel: (NSString *)newText   {
        // If the new value is different than what is already set, 
        // and the label isn't nil, then trigger the animation
        if (self.myLabel.text && self.myLabel.text != newText)    {
            [UIView animateWithDuration...];
        }
        self.myLabel.text = newText;
    }

然后在tableView的cellForRowAtIndexPath中调用该方法。 棘手的是,随着用户滚动或数据更改(即每次调用reloadData),cellForRowAtIndexPath都可以被调用多次。

我通过跟踪myLabel.text保存的先前值设法使其工作。

我在我的viewController上创建了一个NSMutableDictionary ,命名为previousStrings 每个键都是indexPath.row ,每个值都是该行的字典。

这是来自cellForRow方法内部的一些代码。

NSMutableDictionary *strings;
// check to see if previousStrings already has a dictionary at key of indexPath.row
// if not, then create one.
if (![_previousStrings objectForKey:[NSString stringWithFormat:@"%d", indexPath.row]]) {
    strings = [[NSMutableDictionary alloc] init];
}
else {
    strings = [_previousStrings objectForKey:[NSString stringWithFormat:@"%d", indexPath.row]];
}

然后更改myLabel.text

NSString *cellText = newTextString;
// If the new string is equal to the old string, just set the label without animation
// otherwise run animation and update the value inside the strings dictionary.
if ([strings[@"myLabel"] isEqualToString:newTextString]) {
    cell.myLabel.text = newTextString;
}
else {
    [cell.myLabel runTextChangeAnimationWithString:newTextString];
    [strings setObject:newTextString forKey:@"myLabel"];
}

毕竟,在cellForRow的末尾,请确保将字符串字典放回previousStrings中

[_previousStrings setObject:strings forKey:[NSString stringWithFormat:@"%d", indexPath.row]];

我希望这对于在同一问题上绊脚石的其他人来说都是有意义的。 如果不够清晰,请随时进行编辑并提出问题。

这就是字典的结构。 数字是每个单元格的indexPaths。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM