1

I am trying to assign the datasource to a datasource property of a custom UIView which has a UITableView subView. The code is as followed:

CustomUIView.m

- (void)setDataSource:(id<UITableViewDataSource>)dataSource
{
    self.dataSource = dataSource;
    if (!self.tableView) {
        [self createTableView];
        [self setUpTableViewSizeAndAddToSuperView];
    }

    self.tableView.dataSource = self.dataSource;
}

CustomUIView.h

@property (nonatomic, strong) id<UITableViewDataSource> dataSource;

The datasource is provided in a property called delegateobject in a UIViewController.

UIViewController.m

self.customView.dataSource = delegateobject;

UIViewController.h

@property (nonatomic, strong) DelegateObject *delegateobject;

DelegateObject is just a NSObject with UITableViewDataSource protocol.

But when I ran the app it gave me the EXEC_BAD_ACCESS error and crashed. The datasource property of my custom UIView has been released too many times and stopped at this line:

self.dataSource = dataSource;

I have enabled zombie objects diagnosis but couldn't see any additional information. How can I fix this?

4

1 回答 1

1

当你打电话时self.dataSource = dataSource,你正在创建一个无限循环。当您覆盖 setter 时,您需要直接设置实例变量,因此将该行更改为:

_dataSource = dataSource;
于 2013-07-04T08:05:22.013 回答