Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有关全局的 tableViewEstimatedHeightEnabled 属性问题 #252

Closed
4 tasks done
lexiaoyao20 opened this issue Dec 13, 2017 · 11 comments
Closed
4 tasks done

有关全局的 tableViewEstimatedHeightEnabled 属性问题 #252

lexiaoyao20 opened this issue Dec 13, 2017 · 11 comments
Labels

Comments

@lexiaoyao20
Copy link

lexiaoyao20 commented Dec 13, 2017

请填写运行环境

  • 设备:iPhone / iPad / 模拟器
  • 系统:iOS 11.x
  • Xcode 版本:9.x
  • QMUI iOS 版本:2.x.x

请描述具体问题

全局配置里面有个叫 tableViewEstimatedHeightEnabled 属性,然后 UITableView (QMUI) 这个分类里面有如下代码:

- (void)qmui_didMoveToSuperview {
    [self qmui_didMoveToSuperview];
    
    // iOS 11 之后 estimatedRowHeight 默认值变成 UITableViewAutomaticDimension 了,会导致 contentSize 之类的计算不准确,所以这里给一个途径让项目可以方便地禁掉所有 UITableView 的 estimatedXxxHeight
    if (!TableViewEstimatedHeightEnabled) {
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
    } else {
        self.estimatedRowHeight = UITableViewAutomaticDimension;
        self.estimatedSectionHeaderHeight = UITableViewAutomaticDimension;
        self.estimatedSectionFooterHeight = UITableViewAutomaticDimension;
    }
}

我在 ViewControllerviewDidLoad 方法里面如果有如下设置 :

        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;

因为 viewDidLoad 方法会先调用,qmui_didMoveToSuperview 会后调用,而且 tableViewEstimatedHeightEnabled 这个属性默认值为 YES,这样会导致 我 viewDidLoad 方法里面的设置都不生效了,变成了 qmui_didMoveToSuperview 方法里面 else 里面的逻辑。

你可以说我全局 把 TableViewEstimatedHeightEnabled 设为 NO 不就行了吗。但是我这边还是有很多地方有设置 诸如: self.tableView.estimatedRowHeight = 80; ,如果进行了全局设置,不知道会不会对这些逻辑产生影响。

@MoLice
Copy link
Collaborator

MoLice commented Dec 13, 2017

因为 viewDidLoad 方法会先调用,qmui_didMoveToSuperview 会后调用,而且 tableViewEstimatedHeightEnabled 这个属性默认值为 YES,这样会导致 我 viewDidLoad 方法里面的设置都不生效了。

这不是设计初衷,是我们的问题,稍后看看。

@MoLice
Copy link
Collaborator

MoLice commented Dec 13, 2017

你好,刚测了一下无法重现你说的问题,请问你的 viewController superclass 是什么?以及你是怎么添加你的 tableView 到界面上的?

以下是测试结果的截图,第二张图显示在 viewDidLoad 里修改是能生效的。

image

image

@lexiaoyao20
Copy link
Author

viewController 的 superClass 是 UIViewController,tableView 是以代码的方式添加的,比如:


- (void)viewDidLoad {
    [super viewDidLoad];

    [self prepareUI];
}

- (void)prepareUI {
    self.tableView = [[UITableView alloc] init];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    [self.tableView registerClass:[FMWeiBoCell class] forCellReuseIdentifier:CellTableIdentifier];
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.equalTo(self.view);
    }];
  
}

@MoLice
Copy link
Collaborator

MoLice commented Dec 14, 2017

你只需要把那几行代码放到 [self.view addSubview:self.tableView]; 之后就行了

@MoLice
Copy link
Collaborator

MoLice commented Dec 14, 2017

这个问题的原因在于你在init完tableView 就立马去修改它的estimated ,这个时机太早了,如果抛开 QMUI,系统原生的代码,这样写也是不生效的,它要求一个比较晚的时机去修改estimated,这也是为什么QMUI里选择了didMoveToSuper后修改,而不是在init完就修改。

@lexiaoyao20
Copy link
Author

嗯,我明白了,谢谢你的回复!

@MoLice
Copy link
Collaborator

MoLice commented Dec 14, 2017

不客气

@MoLice MoLice closed this as completed Dec 14, 2017
@lexiaoyao20
Copy link
Author

大佬还是有问题,

比如,我现在切换到别的页面,在切换回来,会调用 qmui_didMoveToSuperviewviewDidLoad 方法的设置还是会被覆盖。更严重的是像系统的控件比如 UIPickerView 里面也有用到 UITableView,滚动的时候会产生错位的问题。

强烈建议把这个全局的设置去掉,以及 qmui_didMoveToSuperview 这个方法去掉。。

@lexiaoyao20
Copy link
Author

多说无益,还是直接看图吧:

bug

定位获取到地址之后,我只是简单做了刷新操作,PickerView就出现了错误问题。

[self.pickerView reloadAllComponents];
            [self.pickerView selectRow:[addrs.firstObject integerValue] inComponent:0 animated:YES];
            [self.pickerView selectRow:[addrs.lastObject integerValue] inComponent:1 animated:YES];

现在我只能靠这样才能修复这个问题:

- (void)dealloc
{
    QMUICMI.tableViewEstimatedHeightEnabled = self.originEstimatedHeightEnabled;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //修复错位问题
    self.originEstimatedHeightEnabled = QMUICMI.tableViewEstimatedHeightEnabled;
    QMUICMI.tableViewEstimatedHeightEnabled = NO;
    
    // Load UI
    
}

@MoLice
Copy link
Collaborator

MoLice commented Dec 14, 2017

有道理,我再看看

@MoLice MoLice reopened this Dec 14, 2017
@MoLice MoLice added the bug label Dec 20, 2017
@MoLice
Copy link
Collaborator

MoLice commented Dec 20, 2017

请更新到 2.2.0,已改为在 UITableView initWithFrame:style: 里去应用开关的值,就不会有之前的问题了。

@MoLice MoLice closed this as completed Dec 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants