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

QMUITableViewCell 初始化方法在swift中导致内存问题 #1320

Closed
ziruozimi opened this issue Nov 16, 2021 · 3 comments
Closed

QMUITableViewCell 初始化方法在swift中导致内存问题 #1320

ziruozimi opened this issue Nov 16, 2021 · 3 comments
Labels

Comments

@ziruozimi
Copy link

Bug 表现
QMUITableViewCell 初始化方法

- (instancetype)initForTableView:(UITableView *)tableView withStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self.initByTableView = YES;
    if (self = [self initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.parentTableView = tableView;
        [self didInitializeWithStyle:style];// 因为设置了 parentTableView,样式可能都需要变,所以这里重新执行一次 didInitializeWithStyle: 里的 qmui_styledAsQMUITableViewCell
    }
    return self;
}

在swift中

final class DemoTableViewCell: QMUITableViewCell {
    let label = DemoLabel()
}

label会被初始化两次,当退出页面时还有一个label存在于内存中

截图
image
image
image

如何重现
Demo.zip

预期的表现
如果调用super则表现正常

- (instancetype)initForTableView:(UITableView *)tableView withStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self.initByTableView = YES;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.parentTableView = tableView;
        [self didInitializeWithStyle:style];// 因为设置了 parentTableView,样式可能都需要变,所以这里重新执行一次 didInitializeWithStyle: 里的 qmui_styledAsQMUITableViewCell
    }
    return self;
}

其他信息

  • 设备: [iPhone]
  • iOS 版本: [iOS 14.6]
  • Xcode 版本: [Xcode 12.5.1]
  • QMUI 版本: [4.3.0]
@MoLice MoLice added the bug label Nov 28, 2021
@MoLice
Copy link
Collaborator

MoLice commented Nov 29, 2021

已发布 4.4.0 修复该问题。

@MoLice MoLice closed this as completed Nov 29, 2021
@MoLice MoLice reopened this Dec 8, 2021
@MoLice
Copy link
Collaborator

MoLice commented Dec 8, 2021

发现 4.4.0 的修改方式会导致以下问题:

业务某个 cell 继承自 QMUITableViewCell。当业务要重写 init 方法时,他沿用系统默认的思路,去重写 initWithStyle:reuseIdentifier: 方法,然后在业务 vc 里使用这个 cell 时又是用 QMUI 提供的 initForTableView:withStyle: 系列方法去初始化。

如果这种场景,在升级到 4.4.0 后就会发现 initForTableView 调用时无法触发 cell 内部重写的 initWithStyle:。

@MoLice
Copy link
Collaborator

MoLice commented Dec 14, 2021

已发布 4.4.1,回退该修改,这种情况目前暂无合适的建议,可以考虑业务换个写法。

@MoLice MoLice closed this as completed Dec 14, 2021
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