Skip to content

Commit 4073db6

Browse files
committed
Fix empty view setup if configured before parent view
Before this change, if you were to assign a data source’s emptyView *before* you assigned its tableView or collectionView, the emptyView would not be displayed if there were no items. This was because the emptyView was not [properly updated since at the time of the assignment its parent view would be nil][1]. Before: ```objc dataSource.emptyView = [MyAwesomeView new]; // other stuff dataSource.tableView = self.tableView; // if dataSource has no items, MyAwesomeView will never appear :( ``` After: ```objc dataSource.emptyView = [MyAwesomeView new]; // other stuff dataSource.tableView = self.tableView; // if dataSource has no items, MyAwesomeView will appear in all of its // glory :) ``` [1]: https://github.com/splinesoft/SSDataSources/blob/2321bc79e9a9b2a537ffe8e806b08fbc021b13e2/SSDataSources/SSBaseDataSource.m#L248-L250
1 parent 64c0b70 commit 4073db6

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

Example/ExampleSSDataSourcesTests/SSBaseDataSourceTests.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,21 @@ - (void)testBasicEmptyViewVisibility
204204
expect(arrayDataSource.emptyView.hidden).to.beFalsy();
205205
}
206206

207+
- (void)testEmptyViewSetUpIsNotDependentOnParentViewConfiguration
208+
{
209+
// The intent is to ensure that you can set up your emptyView *before* you
210+
// assign the data source’s table or collection view.
211+
SSArrayDataSource *arrayDataSource;
212+
213+
arrayDataSource = [[SSArrayDataSource alloc] initWithItems:@[]];
214+
arrayDataSource.emptyView = [UIView new];
215+
arrayDataSource.tableView = tableView;
216+
expect(arrayDataSource.emptyView.hidden).to.beFalsy();
217+
218+
arrayDataSource = [[SSArrayDataSource alloc] initWithItems:@[]];
219+
arrayDataSource.emptyView = [UIView new];
220+
arrayDataSource.collectionView = collectionView;
221+
expect(arrayDataSource.emptyView.hidden).to.beFalsy();
222+
}
223+
207224
@end

SSDataSources/SSBaseDataSource.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ - (void)setTableView:(UITableView *)tableView {
104104
if (tableView) {
105105
tableView.dataSource = self;
106106
}
107+
108+
[self _updateEmptyView];
107109
}
108110

109111
- (void)setCollectionView:(UICollectionView *)collectionView {
@@ -112,6 +114,8 @@ - (void)setCollectionView:(UICollectionView *)collectionView {
112114
if (collectionView) {
113115
collectionView.dataSource = self;
114116
}
117+
118+
[self _updateEmptyView];
115119
}
116120

117121
#pragma mark - UITableViewDataSource

0 commit comments

Comments
 (0)