简体   繁体   English

如何设置contentOffset:在insertRowAtIndexPath之后:

[英]How to setContentOffset: after insertRowAtIndexPath:

I developing an app for iOS 5. 我为iOS 5开发了一个应用程序。

How I can set the contentOffset after a row is inserted ( insertRowAtIndexPath: ) in a table view? 在表格视图中插入行( insertRowAtIndexPath: contentOffset后,如何设置contentOffset insertRowAtIndexPath: sets the offset to 0. I have tried to set the offset in willDisplayRow: delegate method but without result. insertRowAtIndexPath:将偏移量设置为0。我试图在willDisplayRow:委托方法中设置偏移量,但是没有结果。

If the issue that you you want to change the offset after the row insertion animation has completed, then call a method to set the offset after delay. 如果您要在完成行插入动画之后更改偏移量的问题,请调用一种方法来设置延迟后的偏移量。 This will let the current run loop finish, allowing for the row insertion animation to complete. 这将使当前的运行循环完成,从而允许行插入动画完成。 You may want to disable interaction with the table view during the delay. 您可能希望在延迟期间禁用与表视图的交互。

You could try adding something like the following to your tableViewController. 您可以尝试将以下内容添加到tableViewController中。

- (void) adjustsContentOffset: (NSIndexPath*) indexPath;
{
    CGFloat offsetY = 0.0; // insert some calculation deriving your value from indexPath
    [[self tableView] setContentOffset: CGPointMake(0.0, offsetY) animated: YES];
}

- (void) insertRow: (NSIndexPath*) indexPath;
{
    [[self tableView] insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    [self performSelector: @selector(adjustsContentOffset:) withObject: indexPath afterDelay: 0.3];
}

Thanks Matthew again for your really helpful answer but finally I found another solution that i want to share with community: 再次感谢马修(Matthew)的宝贵帮助,但最后我找到了另一个要与社区分享的解决方案:

Just add a empty cell at the end of a table with height that equals your desired offset 只需在表格末尾添加一个空单元格,其高度等于所需的偏移量

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

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