我正在使用Parse
将数据导入简单的Table View
。如果没有数据,我会通过此显示label
:
- (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;
}
我无法弄清楚如何在WatchKit
中做同样的事情,因为Table View
的工作有点不同。但我只是在iPhone
视图中显示与Watch
视图相同的数据。
以下是我WatchKit
InterfaceController.m
中的内容。我需要在TableViewController.m
中添加类似的逻辑,以便在没有数据的情况下添加“无数据”标签,任何想法如何在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);
}
}
编辑:根据Skylar Lauren的回答添加。仍然没有标签的空白WatchKit场景。
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;
}
答案 0 :(得分:2)
最简单的解决方案是在故事板中为您的WatchKit视图添加一个名为noDataAvailableLabel的标签,并将文本设置为“No data available”,并隐藏myTable或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;
}
另一个选项是添加一个名为noData的新行类型,并使用它而不是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"];
}