在数组块枚举中创建 UITableViews 导致崩溃 [英] creating UITableViews in array block enumeration causes crash

查看:63
本文介绍了在数组块枚举中创建 UITableViews 导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以故事是这样的:)

我试图阻止 NSArray 中的枚举对象,并为每个对象动态创建 UITableViews 并将它们添加到 UIScrollView 中.我正在使用 来自 www.objc.io 的更轻的视图控制器 为了可读性和可重用性.分别为每个 UITableView 创建数据源.问题是我总是崩溃

i am trying to block enumerate objects in an NSArray and dynamically create UITableViews for each of them and add them in UIScrollView. i am using Lighter View Controllers from www.objc.io for the sake of readability and reusability. the dataSource gets created for each UITableView separately. the problem is i crash all the time with

-[NSObject(NSObject) doesNotRecognizeSelector:]

我从堆栈上的帖子中发现,由于速度问题,块枚举中的对象保留很弱,并且可以确认数据源实际上已为每个表释放.

i found out from posts on stack that objects in block enumeration are weak retained for speed concerns and can confirm that the dataSource gets actually deallocated for each table.

我什至尝试用 __strong 初始化 ArrayDataSource 但没有效果.

i even tried to init ArrayDataSource with __strong but with no effect.

__strong ArrayDataSource *customdayTableDataSource = [[ArrayDataSource alloc] initWithConfigureCellBlock:configureCell cellIdentifier:DayTableCellIdentifier];

我在街区做错了什么?你能指出我正确的方向吗?

what am i doing wrong in the block? can you please point me the right direction?

TableViewCellConfigureBlock configureCell = ^(id cell, id object) {
    [cell configureForObject:object];
};

[NSArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    int tableHorizontalPosition = [[UIScreen mainScreen] bounds].size.width * idx;               
    int tableHeight = [[UIScreen mainScreen] bounds].size.height; 

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(tableHorizontalPosition, 0, [[UIScreen mainScreen] bounds].size.width, tableHeight) style:UITableViewStylePlain];

    [table setDelegate:self];

    ArrayDataSource *customDataSource = [[ArrayDataSource alloc] initWithConfigureCellBlock:configureCell cellIdentifier:MyCellIdentifier];

    [customTableDataSource setOriginalData:[NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]];

    [table setDataSource:customTableDataSource];

    [[self myUIScrollView] addSubview:table];

}];

正如 rmaddy 所指出的,我将每个数据源添加到一个 NSArray 中,该数组在块的范围之外初始化.这解决了我的问题.谢谢

as pointed out with rmaddy i added each dataSource to an NSArray initialized out of the scope of the block. this solved my problem. thank you

推荐答案

正如人们在评论中所说,你必须在一个地方创建你的数据源,只要 tableviews,它们就会活着",或者更简单的解决方案是创建使用 objc_setAssociatedObject 的强引用

As people said in comments, you must create your datasorces somewhere where they will be "alive" as long as the tableviews, or maybe easier solution would be to create strong reference using objc_setAssociatedObject

在你的类中声明一些static char strongReferenceKey

并在您的块中,设置数据源后,执行:

and in your block, after setting datasource, do:

objc_setAssociatedObject(table, &strongReferenceKey, customDataSource, OBJC_ASSOCIATION_RETAIN);

这样表就会对数据源有强引用,当表被释放时会被释放.

That way table will have strong reference on the datasource, which will be released when table gets deallocated.

附言确保导入 runtime.h:

P.S. Make sure you import runtime.h:

#import <objc/runtime.h>

这篇关于在数组块枚举中创建 UITableViews 导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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