diff --git a/Code/UI/RKTableController.m b/Code/UI/RKTableController.m index 580845eb..eef6248f 100644 --- a/Code/UI/RKTableController.m +++ b/Code/UI/RKTableController.m @@ -314,6 +314,9 @@ if (self.sectionNameKeyPath) { NSArray *sectionedObjects = [objects sectionsGroupedByKeyPath:self.sectionNameKeyPath]; + if ([sectionedObjects count] == 0) { + [self removeAllSections]; + } for (NSArray *sectionOfObjects in sectionedObjects) { NSUInteger sectionIndex = [sectionedObjects indexOfObject:sectionOfObjects]; if (sectionIndex >= [self sectionCount]) { diff --git a/Tests/Application/UI/RKTableControllerTest.m b/Tests/Application/UI/RKTableControllerTest.m index aaa33600..0378a792 100644 --- a/Tests/Application/UI/RKTableControllerTest.m +++ b/Tests/Application/UI/RKTableControllerTest.m @@ -706,6 +706,43 @@ assertThatInt(tableController.rowCount, is(equalToInt(3))); } +- (void)testLoadingACollectionOfObjectsIntoSectionsAndThenLoadingAnEmptyCollectionChangesTableToEmpty { + RKObjectManager* objectManager = [RKTestFactory objectManager]; + objectManager.client.cachePolicy = RKRequestCachePolicyNone; + RKTableControllerTestTableViewController* viewController = [RKTableControllerTestTableViewController new]; + RKTableController* tableController = [RKTableController tableControllerForTableViewController:viewController]; + tableController.objectManager = objectManager; + [tableController mapObjectsWithClass:[RKTestUser class] toTableCellsWithMapping:[RKTableViewCellMapping cellMappingUsingBlock:^(RKTableViewCellMapping* mapping) { + mapping.cellClass = [RKTestUserTableViewCell class]; + [mapping mapKeyPath:@"name" toAttribute:@"textLabel.text"]; + [mapping mapKeyPath:@"nickName" toAttribute:@"detailTextLabel.text"]; + }]]; + tableController.sectionNameKeyPath = @"name"; + RKTableControllerTestDelegate* delegate = [RKTableControllerTestDelegate tableControllerDelegate]; + delegate.timeout = 10; + tableController.delegate = delegate; + [tableController loadTableFromResourcePath:@"/JSON/users.json" usingBlock:^(RKObjectLoader* objectLoader) { + objectLoader.objectMapping = [RKObjectMapping mappingForClass:[RKTestUser class] usingBlock:^(RKObjectMapping* mapping) { + [mapping mapAttributes:@"name", nil]; + }]; + }]; + [delegate waitForLoad]; + assertThatBool([tableController isLoaded], is(equalToBool(YES))); + assertThatInt(tableController.sectionCount, is(equalToInt(3))); + assertThatInt(tableController.rowCount, is(equalToInt(3))); + delegate = [RKTableControllerTestDelegate tableControllerDelegate]; + delegate.timeout = 10; + tableController.delegate = delegate; + [tableController loadTableFromResourcePath:@"/204" usingBlock:^(RKObjectLoader* objectLoader) { + objectLoader.objectMapping = [RKObjectMapping mappingForClass:[RKTestUser class] usingBlock:^(RKObjectMapping* mapping) { + [mapping mapAttributes:@"name", nil]; + }]; + }]; + [delegate waitForLoad]; + assertThatBool([tableController isLoaded], is(equalToBool(YES))); + assertThatBool([tableController isEmpty], is(equalToBool(YES))); +} + #pragma mark - RKTableViewDelegate Tests - (void)testNotifyTheDelegateWhenLoadingStarts { diff --git a/Tests/Server/server.rb b/Tests/Server/server.rb index 96ec0550..3169a844 100644 --- a/Tests/Server/server.rb +++ b/Tests/Server/server.rb @@ -99,6 +99,12 @@ class RestKitTestServer < Sinatra::Base content_type 'application/json' "".to_json end + + get '/204' do + status 204 + content_type 'application/json' + "".to_json + end get '/403' do status 403