I'm using Parse
to pull in data into a simple Table View
. When there is no data I show a label
via this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if ([self.stringArray count] > 0)
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
self.tableView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height)];
noDataLabel.text = @"No data available";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
self.tableView.backgroundView = noDataLabel;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
I can't figure out how to do the same thing in WatchKit
since Table View
s work a bit different. But I'm just showing the same data in the iPhone
view as the Watch
view.
Here is what I have in my WatchKit
InterfaceController.m
. I need to add a similar logic to what I do in the TableViewController.m
for adding a "No Data" label if there's no data, any ideas how that would work in WatchKit
?
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
[self.myTable setNumberOfRows:[self.data count] withRowType:@"myRows"];
for (NSInteger index = 0; index < [self.data count]; index++) {
NSString *title = [self.data objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel setText:title];
NSLog(@"Title: %@", title);
}
for (NSInteger index = 0; index < [self.dataDate count]; index++) {
NSString *titleDate = [self.dataDate objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel2 setText:titleDate];
NSLog(@"TitleDate: %@", titleDate);
}
}
EDIT: Added per Skylar Lauren answer. Still getting a blank WatchKit scene with no label though.
if ([self.data count] > 0)
{
UILabel *noDataAvailableLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 15, 15)];
noDataAvailableLabel.hidden = YES;
self.myTable.hidden = NO;
[self.myTable setNumberOfRows:[self.data count] withRowType:@"myRows"];
for (NSInteger index = 0; index < [self.data count]; index++) {
NSString *title = [self.data objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel setText:title];
NSLog(@"Title: %@", title);
}
for (NSInteger index = 0; index < [self.dataDate count]; index++) {
NSString *titleDate = [self.dataDate objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel2 setText:titleDate];
NSLog(@"TitleDate: %@", titleDate);
}
}
else
{
UILabel *noDataAvailableLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 15, 15)];
self.myTable.hidden = YES;
noDataAvailableLabel.hidden = NO;
noDataAvailableLabel.text = @"No data available";
noDataAvailableLabel.textColor = [UIColor blackColor];
noDataAvailableLabel.textAlignment = NSTextAlignmentCenter;
}
The simplest of solutions would be to add a label to your WatchKit view in your storyboard called noDataAvailableLabel and set the text to "No data available" and just either hide the myTable or the noDataAvailableLabel.
if ([self.data count] > 0])
{
self.noDataAvailableLabel.hidden = YES;
self.myTable.hidden = NO;
[self.myTable setNumberOfRows:[self.data count] withRowType:@"myRows"];
for (NSInteger index = 0; index < [self.data count]; index++) {
NSString *title = [self.data objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel setText:title];
NSLog(@"Title: %@", title);
}
for (NSInteger index = 0; index < [self.dataDate count]; index++) {
NSString *titleDate = [self.dataDate objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel2 setText:titleDate];
NSLog(@"TitleDate: %@", titleDate);
}
}
else
{
self.myTable.hidden = YES;
self.noDataAvailableLabel.hidden = NO;
}
The other option is to add a new row type called noData and use that instead of myRows.
if ([self.data count] > 0])
{
[self.myTable setNumberOfRows:[self.data count] withRowType:@"myRows"];
for (NSInteger index = 0; index < [self.data count]; index++) {
NSString *title = [self.data objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel setText:title];
NSLog(@"Title: %@", title);
}
for (NSInteger index = 0; index < [self.dataDate count]; index++) {
NSString *titleDate = [self.dataDate objectAtIndex:index];
MyRowController *controller = [self.myTable rowControllerAtIndex:index];
[controller.myLabel2 setText:titleDate];
NSLog(@"TitleDate: %@", titleDate);
}
}
else
{
[self.myTable setNumberOfRows:1 withRowType:@"noData"];
}