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

时间:2015-02-18 00:20:23

标签: ios objective-c uitableview 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视图相同的数据。

enter image description here

以下是我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;
}

1 个答案:

答案 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"];
}