上滚动的UITableView奇怪的行为 [英] Strange behavior on scroll UITableView

查看:148
本文介绍了上滚动的UITableView奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套的滚动和表视图这种分层树的单一视图:

(表装入此的例如。)

在viewWillAppear中以编程方式滚动在最后一排表视图做

   - (无效)viewWillAppear中:(BOOL)动画
{
   [self.tableView reloadData];
   NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0] -1切入口:0];
   [self.tableView scrollToRowAtIndexPath:indexPath
                      atScrollPosition:UITableViewScrollPositionBottom
                              动画:YES];
}

一切正常,在这两个方向。正如你所看到的,文字是正确截断。


我在Github上上传了我的项目,所以你可以看到它运行。在这里下载

现在,如果你cellForRowAtIndexPath方法中添加此行:

  cell.textLabel.numberOfLines = 0;

细胞自动将调整嵌入和显示整个文本标签:

但,这是问题我亲爱的朋友,你可以看到的表没有被滚动到最后一排,但5-6行上述。我不明白为什么。

同样的事情也发生如果NumberOfLines是2,3等。只有值1可以使TableView中向下移动到最后一行。

(仅供参考,我不使用 [self.tableView setContentOffset:CGPointMake(0,CGFLOAT_MAX); 为了因为它摔倒滚动表一个例外)

或许真的是错的iOS8上?


解决方案

viewDidLayoutSubviews将做的工作

我猜你用一些自动布局或自我大小的细胞?在 viewWillAppear中的计算还没有完成。所以你滚动至底部,但其后调整大小自动版式你的细胞,是什么让他们在高度扩张。


  

有关详细信息:<一href=\"https://developer.apple.com/library/ios/documentation/Uikit/reference/UIViewController_Class/index.html\"相对=nofollow>请参考苹果文档的UIViewController


  
  

...你的视图控制器可以重写此方法进行修改视图勾画出它的子视图后,此方法的默认实现不做任何事情。


   - (无效){viewDidLayoutSubviews
    //你的code在这里
}

I have a single view with nested scroll and table view with this hierarchical tree:

(The table is loaded with some Huckleberry Finn rows taken from this example.)

On viewWillAppear I programmatically scroll the Table View at the last row doing

-(void)viewWillAppear:(BOOL)animated
{
   [self.tableView reloadData];
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0]-1 inSection:0];
   [self.tableView scrollToRowAtIndexPath:indexPath
                      atScrollPosition:UITableViewScrollPositionBottom
                              animated:YES];
}

and everything works as expected, in both orientation. As you can see, the text is rightly truncated.

I've uploaded on Github my project so you can see it running. Download it here.

Now, if you add this line in the cellForRowAtIndexPath method:

cell.textLabel.numberOfLines=0;

the cells automatically will resize to embed and show the entire text label:

BUT, and this is the issue my dear friends, as you can see the table is not scrolled to the last row but 5-6 rows above. And I cannot understand why.

The same thing happens also if NumberOfLines is 2,3 and so on. Only a value of 1 can make the TableView move down to the very last row.

(FYI, I do not use [self.tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; in order to scroll down the table because it throws an exception)

Maybe something is wrong with iOS8 ?

解决方案

viewDidLayoutSubviews will do the job

I guess you use some autolayout or self sizing cells? In viewWillAppear the calculation is not finished. So you scroll to the bottom, but then thereafter autolayout resizes your cells what makes them expand in height.

For Details: Refer to apple documentation UIViewController

... "Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing."

- (void)viewDidLayoutSubviews {
    // your code here
}

这篇关于上滚动的UITableView奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆