UITableView setDataSource – Disallowed Object Error

I have a UIViewController
which contains this property:

@property (weak, nonatomic) IBOutlet UITableView *customerTableView;

In the UIViewController’s viewDidLoad
method, I have the following:

- (void)viewDidLoad
    [super viewDidLoad];
    // ...
    CustomerTableViewDataSource *dataSource = [[CustomerTableViewDataSource alloc]
    [dataSource setData:customersSource];
    [customerTableView setDataSource:dataSource];

I receive this with NSZombies enabled:

*** -[CustomerTableViewDataSource numberOfSectionsInTableView:]: message sent to
deallocated instance 0x3e07a0

The deallocated instance address is the same as the data source.

Is there any reason why the data source isn’t ‘sticking’?

Using the simulator and Zombies, I see that the reference is being released in viewDidLoad
, which I suppose is no surprise since I declared it in that method and it should fall out of scope, however I would think that calling setDatasource
would increase the refct. Well, it apparently doesn’t.

Is there any way I can change this behavior?

Note: Everything works
if I create an ivar and store the data source, but I feel like that clutters my class. Is this just life in the city?

From the documentation
of UITableView

@property(nonatomic, assign) id dataSource


The data source must adopt the UITableViewDataSource protocol. The data source is not retained.

This means that customerTableView
expects you to retain your data source object, which you did not do. Same goes for UITableView
‘s delegate (in fact, for almost any delegate in cocoa, except CALayer
‘s one). This design decision is made for a reason: the object that holds on to UITableView
very often also serves as its delegate. Had the delegate been retained, the programmers would have to deal with retain cycles.

Unfortunately, the consequence of that decision is the problem that you are having: now you need to retain the data source (and the delegate, if any), e.g. by setting up an ivar.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » UITableView setDataSource – Disallowed Object Error

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录