如果数组中没有项目,则显示无数据标签(WatchKit)

If no items in Array, show No Data Label (WatchKit)

我正在使用 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 中添加 "No Data" 标签类似的逻辑,知道在 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;
}

最简单的解决方案是在故事板中为 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"];
}