- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我有一个非常简单的 ARC 测试应用程序。其中一个 View Controller 包含 UITableView。在制作行动画(insertRowsAtIndexPaths
或 deleteRowsAtIndexPaths
)之后,UITableView(和所有单元格)从未被释放。如果我使用 reloadData
,它工作正常。在 iOS 6 上没有问题,只有 iOS 7.0。关于如何修复此内存泄漏的任何想法?
-(void)expand {
expanded = !expanded;
NSArray* paths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0],nil];
if (expanded) {
//[table_view reloadData];
[table_view insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationMiddle];
} else {
//[table_view reloadData];
[table_view deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationMiddle];
}
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return expanded ? 2 : 0;
}
table_view 是一种 TableView 类(UITableView 的子类):
@implementation TableView
static int totalTableView;
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if (self = [super initWithFrame:frame style:style]) {
totalTableView++;
NSLog(@"init tableView (%d)", totalTableView);
}
return self;
}
-(void)dealloc {
totalTableView--;
NSLog(@"dealloc tableView (%d)", totalTableView);
}
@end
最佳答案
好吧,如果你挖掘得更深一点(禁用 ARC,子类 tableview,重写 retain/release/dealloc 方法,然后在它们上面放置日志/断点),你会发现动画完成 block 中发生了一些不好的事情,这可能导致泄漏。
看起来 tableview 在 iOS 7 上插入/删除单元格后从完成 block 接收了太多保留,但在 iOS 6 上没有(在 iOS 6 上 UITableView 尚未使用 block 动画 - 你也可以在堆栈跟踪上检查它) .
所以我尝试以一种肮脏的方式从 UIView 接管 tableview 的动画完成 block 生命周期:方法调配。 这实际上解决了问题。
但它的功能更多,所以我仍在寻找更复杂的解决方案。
所以扩展 UIView:
@interface UIView (iOS7UITableViewLeak)
+ (void)fixed_animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
+ (void)swizzleStaticSelector:(SEL)selOrig withSelector:(SEL)selNew;
@end
#import <objc/runtime.h>
typedef void (^CompletionBlock)(BOOL finished);
@implementation UIView (iOS7UITableViewLeak)
+ (void)fixed_animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion {
__block CompletionBlock completionBlock = [completion copy];
[UIView fixed_animateWithDuration:duration delay:delay options:options animations:animations completion:^(BOOL finished) {
if (completionBlock) completionBlock(finished);
[completionBlock autorelease];
}];
}
+ (void)swizzleStaticSelector:(SEL)selOrig withSelector:(SEL)selNew {
Method origMethod = class_getClassMethod([self class], selOrig);
Method newMethod = class_getClassMethod([self class], selNew);
method_exchangeImplementations(origMethod, newMethod);
}
@end
如您所见,原始完成 block 并未直接传递给 animateWithDuration: 方法,而是从包装器 block 中正确释放(缺少此 block 会导致 TableView 泄漏)。我知道它看起来有点奇怪,但它解决了问题。
现在用您 App Delegate 的 didFinishLaunchingWithOptions 或任何您想要的新动画实现替换原来的动画实现:
[UIView swizzleStaticSelector:@selector(animateWithDuration:delay:options:animations:completion:) withSelector:@selector(fixed_animateWithDuration:delay:options:animations:completion:)];
之后,所有对 [UIView animateWithDuration:...]
的调用都会导致这个修改后的实现。
关于iOS 7.0 和 ARC : UITableView never deallocated after rows animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18919870/
首先我要说的是,我主要是一名 iOS 开发人员,最近才开始探索全栈开发的惊心动魄的冒险。我是 PHP 和 MySQL 的新手。话虽如此,我有一个问题,我希望需要一个直接的解决方案。 我正在为一个拥有预
我的表单上有 GridEx 对象并且... 我想用 for...next 循环在其中添加一些项目。实际上我找不到任何方法来添加带有自定义数据的新行。 我想在那个 GridEx 对象中选择一个特定的行。
我有以下数据框 df1 = DataFrame([['OBJ1', 10, 'BX', 'pool1', 'OBJ2'],['OBJ2', 0, '', '', 'OBJ1'],['OBJ3', 1
所以我有以下成员(member)历史表 User_ID | Start date | End Date | Type(0-7) | ---------------------------
我不擅长sql。我查看了 stackoverflow,但似乎对我没有任何用处。所以,我正在寻求帮助。是否可以根据行中的第一个字段将 2 行合并为 1 行。 我在 mysql 中执行此操作。我将展示示例
我了解 flex sm、md、lg 列的概念,但不了解应用于行的概念。弹性行有什么作用? sm、md、lg 尺寸应用于弹性行时意味着什么? 最佳答案 简答 - .row只是网格的容器 col .然而,
我遇到麻烦的地方 我相信我需要使用 COUNT;但是,我不知道如何将一行与同一列中的每一行进行比较,然后计算有多少行比相应行少/便宜。提前谢谢您! 这是我试图解决的官方问题: “使用示例架构,编写一条
我有以下 3 个相关表 Schools Departments Classes --------------- ------------------ --
我有代码: g, g_err = data[:, 4:6].T 我不知道[:, 4:6]的含义 尤其是第一个: .T 表示转置吗? 最佳答案 您有一个名为 data 的二维矩阵,您的代码从第一维获取所
我在行单击上附加了一个事件,在行内的复选框更改上附加了一个事件。 如何防止首先触发行单击? $(document).on('click', 'table tr', function() { con
我有一个场景,我需要连接两个 SQL 表并且正在为如何做而苦恼。假设在表 A 中我有这个: ColA ColB ColC ColD 45 55 17 45 45
我用谷歌表格记录我们俱乐部的出席率。该表格链接到另一个谷歌表格,因此可以自动创建一个名字列表,并按字母顺序排序。在这张表格中,我还根据我们所做的活动,在人的名字旁边手动输入点数。。问题是,由于姓名列表
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个回答) 5年前关闭。 编辑:我正在使用此代码读取 Ex
我是 R 的初学者。我希望你能帮助我解决我的问题。我的数据集中的文件名包含大量信息。我必须提取这些信息来创建单独的变量。 首先我使用 splits <- t(as.data.frame(strspli
假设我有一个 CSR 格式的矩阵,将一行(或多行)设置为零的最有效方法是什么? 下面的代码运行得很慢: A = A.tolil() A[indices, :] = 0 A = A.tocsr() 我不
我遇到了一个相当复杂的问题。我有一个包含三行的数据框:id、info 和 rownum。数据如下所示: id info row 1 a 1 1 b 2
我正在阅读learnjsdata.com并遇到了这种不熟悉的 JavaScript 语法。语法如下: lookupIndex[row[lookupKey]] = row; 有人知道这里发生了什么吗?我
我有一个表格,收集每周足球比赛的结果。 根据每场比赛的结果,我根据 ID 为玩家输入 3、1 或 0 分,具体取决于他们是赢、平还是输。 id Name A1 B1 C1 A2 B2
我有一个 mysql 表: 在表1中,我有3列:Bidang、Keahlian、Nilai。在 saran 列的表结果中,我想根据 bidang 组合 keahlian,但如果 nilai > 0,则
我有一个看起来像这样的表 | ID | item1 | item 2 | | 1 | A1 | B1 | | 2 | A2 |
我是一名优秀的程序员,十分优秀!