gpt4 book ai didi

ios - 如何在短时间内将新插入的 UITableViewCell 的 alpha 设置为 0

转载 作者:行者123 更新时间:2023-11-28 22:16:06 24 4
gpt4 key购买 nike

我正在创建一个将 UITableView 向下滚动到底部的效果,创建一个 UILabel 并将其放置在 UITextView 的顶部,其中用户输入了他们的文本,然后将该 UILabel 动画化到新的 UITableViewCell 的 框架上。这与在 iOS7 的默认短信应用程序中发送新消息时使用的效果大致相同。除了一个部分,我把所有东西都放下了,那就是将新创建的 UITableViewCell 的 alpha 设置为 0,所以它看起来像空白区域,因为 UILabel 正在动画化它。我尝试了以下方法:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_randomData.count-1 inSection:0];
CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.textLabel setAlpha:0.0f];

这里的问题是,由于 UITableView 向下滚动到底部并插入单元格,任何时候我尝试访问特定的 UITableViewCell 它都是空的。如果有帮助,动画由以下方式触发:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

这是因为一旦用户点击“发送”按钮,我就会使用 scrollToRowAtIndexPath 将用户带到表格 View 的底部:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_randomData.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

我尝试在 cellForRowAtIndexPath 中设置 alpha,但由于所有单元格都在 UITableView 向下滚动时重新创建,所以我设置了所有重用的 UITableViewCell's alpha 也为 0,这显然是行不通的。

总而言之,我真的只想将新创建的单元格的 alpha 设置为 0,然后在动画完成后将其设置回 1。有什么想法吗?

谢谢!

最佳答案

首先,您应该设置 alpha 和动画以下方法(参见文档:https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:willDisplayCell:forRowAtIndexPath :)

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

为了产生类似的效果,我必须在我的模型中存储一个值,告诉我我的单元格是否是新创建的单元格,并在上面的函数中使用它,如下所示:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
MyModelItem* item = [MyModel itemAtIndex:indexPath.row];
if(item.wasJustCreated) {
item.wasJustCreated = NO;
cell.alpha = 0.0;
[UIView animateWithDuration:0.4 animations:^{
cell.alpha = 1.0;
}];
}
}

您将不得不在您的单元格模型项中添加一个字段,例如“wasJustCreated”,该字段被初始化为 NO。它可能不是最优雅的解决方案,但可以按预期工作并且易于实现。

关于ios - 如何在短时间内将新插入的 UITableViewCell 的 alpha 设置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21633754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com