【问题标题】:How to postion tableView to the last inserted section SMOOTH如何将表格视图定位到最后插入的部分
【发布时间】:2015-05-01 22:54:05
【问题描述】:

编辑:

我找到了这个 SO 线程。

UITableView inserting section at top while scrolling

  • 我有一个tableView
  • 我最初加载了 5 个部分
  • 然后我向上滚动,当显示第 0 节和第 0 行时,我加载接下来的 5 节并使用
  • 插入表格视图
[self beginUpdates];

[self deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

[self insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

[self insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];

[self insertSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];

[self endUpdates];

但是我的问题是 tableView 在插入后在第 0 节和第 0 行获得位置

但我想定位在插入的最后一个新部分的最后一行(即上面代码中的第 2 部分)

我在开始/结束更新内外都试过这个

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[self scrollToRowAtIndexPath:indexPath
            atScrollPosition:UITableViewScrollPositionBottom animated:YES];

但它会突然发生。

【问题讨论】:

  • [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom Animation:NO];为什么动画属性是“NO”?
  • 我尝试了“是”和“否”...我刚刚在问题中发布了“否”
  • 你可以尝试用延迟“0.4”滚动,看看它做了什么。制作“withRowAnimation:UITableViewRowAnimationFade”。
  • @user2384694 你继承了表格视图吗?
  • @user2384694 你在使用 UITbleViewController 吗?

标签: ios objective-c uitableview


【解决方案1】:

插入部分后,您还可以使用

将表格视图滚动到预期的索引路径
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];

[UIView animateWithDuration: 1.0
                  animations: ^{
                      [tableView scrollToRowAtIndexPath:indexPath
            atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                  }
                  completion: ^(BOOL finished){
                  }
    ];

注意

1) 在[self endUpdates];之后插入上述代码

2) 当您使用 UIView 的 animateWithDuration: animations: completion: API 时,请确保使用 scrollToRowAtIndexPath: atScrollPosition: animated:animated 标志保留为 NO

3) 您还可以根据距离(内容大小、垂直滚动的高度)计算持续时间,即持续时间可以取决于源索引路径和目标索引路径之间的距离。

【讨论】:

    【解决方案2】:
    Please try below code.
    
    [self beginUpdates];
    
    /*
           Insert your sections
    */
    
    [self endUpdates];
    
    [self performSelector:@selector(scroll) withObject:nil afterDelay:0.1];
    
    -(void)scroll 
    {
        if([Your array count] > 0)
        {
            [YOURTABLENAME scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
    
    I Hope above code is work for you.
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多