简体   繁体   English

调用[tableView reloadData]会恢复帧更改

[英]calling [tableView reloadData] reverts frame change

I have a UITableView inside a UIView inside a UIViewController in my app. 我在我的应用程序中的UIViewController内部的UIView有一个UITableView

Above the UITableView , I have a UISearchBar that filters the UITableView . UITableView上方,我有一个过滤UITableViewUISearchBar I do not want to use the conventional UISearchBar as it is a very custom app. 我不想使用传统的UISearchBar因为它是一个非常自定义的应用程序。

I am subscribing to notifications for when the keyboard appears and disappears, and I am resizing the UITableView accordingly. 我正在订阅键盘出现和消失时的通知,我正在调整UITableView大小。 The problem I am having is as soon as I search, and filter the UITableView , I call: 我遇到的问题是,一旦我搜索并过滤UITableView ,我就会调用:

[self.tableView reloadData];

to display the filtered results only. 仅显示筛选结果。 The problem is, the UITableView resizes itself back to its original height , thus hiding a few cells behind the keyboard still. 问题是, UITableView将自身调整回其原始高度 ,从而在键盘后面隐藏了一些单元格。

Can anyone clue me in on how to change this behavior? 任何人都可以告诉我如何改变这种行为?

This sounds like it might be an auto layout problem -- these often show up after some action causes the view to redraw itself. 这听起来像是一个自动布局问题 - 这些问题通常会在某些操作导致视图重绘后出现。 If you're using auto layout, you should resize the table view (or its containing view) by adjusting the constraints, not by setting the frame. 如果您正在使用自动布局,则应通过调整约束来调整表视图(或其包含视图)的大小,而不是通过设置框架。 One way to do that is to make an IBOutlet to the constraint to the bottom of the view, and modify its constant in code. 一种方法是将IBOutlet设置为视图底部的约束,并在代码中修改其常量。

To Debug this, I would create a subclass of UITableView , and override the setBounds: method to see who is modifying it. 要调试它,我将创建UITableView的子类,并覆盖setBounds:方法以查看谁正在修改它。 Once you understand and can fix the problem, you can just throw away your UITableView subclass and go back to using the normal thing. 一旦理解并解决了问题,您就可以丢弃UITableView子类并返回使用正常的东西。

- (void)setBounds:(CGRect)bounds
{
    // Break point here and inspect stack
    [super setBounds:bounds];
}

I had a similar problem -- not related to the keyboard, but I was attempting to resize the table and reload the table data on the same event. 我遇到了类似的问题 - 与键盘无关,但我试图调整表的大小并在同一事件上重新加载表数据。

What seems to work is putting a call to the table resizing method inside of tableCell:cellForRowAtIndexPath . 似乎工作的是调用tableCell:cellForRowAtIndexPath的表调整大小方法。 For efficiency, I've also given the ViewContoller a BOOL regarding whether the table has already been repositioned so that the table isn't repositioned for every single cell that appears. 为了提高效率,我还给ViewContoller一个关于表是否已经重新定位的BOOL ,以便不为每个出现的单元重新定位表。

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

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