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

标签 ios iphone objective-c uitableview

我正在创建一个将 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/

相关文章:

iOS Table View,更改表格索引的框架

ios - 错误 "NSMutableRLEArray objectAtIndex:effectiveRange::Out of bounds'“

iphone - 将一个图像放在另一个图像之上

ios - 为什么不同 TableView Controller 中的 titleForheaderInsection 方法不起作用?

ios - 动态 TableViewCell 高度以适合下载的图像

ios - iOS Simulator App无法启动

ios - MKOverlay 路线在 Apple Breadcrumb iOS 代码中工作,但在我的应用程序中不起作用

ios - 如何在 SpriteKit 中将相机 View 作为我的背景图像?

iphone - 使用用户 token 、rest/rails 和钥匙串(keychain)帮助进行 iOS 身份验证

objective-c - 如何获取在 CTFrame 上绘制的文本的实际高度