Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为UITableView正确配置空视图?_Ios_Objective C_Uitableview_Xcode Storyboard - Fatal编程技术网

Ios 如何为UITableView正确配置空视图?

Ios 如何为UITableView正确配置空视图?,ios,objective-c,uitableview,xcode-storyboard,Ios,Objective C,Uitableview,Xcode Storyboard,我需要设置一个视图,填充UITableView的内容区域,并在该表视图为空时显示,否则隐藏。该视图包含图像、标签和执行简单操作的按钮。我希望实现两件事: 1)利用UITableView的backgroundView属性,以及 2)尽可能利用情节提要,同时最小化代码量(可能使用容器视图?) 然而,我不知道什么是最好的方式去做这件事。非常感谢您的见解,尤其是如果您自己已经实现了此解决方案。您可能可以通过多种方法来实现这一点,但最简单(概念上)的方法似乎是只使用具有两个子视图的容器视图: 非空时可见

我需要设置一个视图,填充
UITableView
的内容区域,并在该表视图为空时显示,否则隐藏。该视图包含图像、标签和执行简单操作的按钮。我希望实现两件事:

1)利用
UITableView
backgroundView
属性,以及

2)尽可能利用
情节提要
,同时最小化代码量(可能使用容器视图?)


然而,我不知道什么是最好的方式去做这件事。非常感谢您的见解,尤其是如果您自己已经实现了此解决方案。

您可能可以通过多种方法来实现这一点,但最简单(概念上)的方法似乎是只使用具有两个子视图的容器视图:

  • 非空时可见的
    UITableView
  • 保存图像/标签/按钮的
    ui视图
    ,当
    UITableView
    为空
只需根据
UITableView


即使您只使用
UITableView
并将其设置为
backgroundView
以在
UITableView
为空时显示
UIView
,也必须完成几乎相同的工作量(您仍然必须创建两个视图)。

您可能可以通过多种方法来完成此操作,但最简单(概念上)似乎只是使用具有两个子视图的容器视图:

  • 非空时可见的
    UITableView
  • 保存图像/标签/按钮的
    ui视图
    ,当
    UITableView
    为空
只需根据
UITableView


即使仅使用
UITableView
并将其
backgroundView
设置为
UITableView
以在
UITableView
为空时显示,也必须完成几乎相同的工作量(您仍然必须创建两个视图).

首先,您要在
viewdiload
中设置背景视图:

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
//Make this clear and it will be the same as the tableView background color
[backgroundView setBackgroundColor:[UIColor clearColor]];
//We can add anything to this text of images
[backgroundView addSubview:imageView];
//This is the important part
[self.tableView setBackgroundView:backgroundView];
现在要隐藏或查看背景视图,您需要在
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
中添加一些逻辑,例如:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Or wherever your data is
    if([self.data count]>0) {

        self.tableView.backgroundView.hidden=YES;
        return 1;

    }

    else {

        self.tableView.backgroundView.hidden=NO;


    }


    return 0;
}
只需检查有多少节,如果表中没有节,则显示视图。如果有分区,则将其隐藏

编辑,显然,如果您愿意,也可以在界面生成器中设置UIView。确保尺寸正确。使用以下内容加载if:

UIView *view = [[[NSBundle mainBundle]
        loadNibNamed:@"MyView"
        owner:self options:nil]
         firstObject];

您需要再次设置它:
[self.tableView setBackgroundView:view]

首先要在
视图加载中设置背景视图:

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
//Make this clear and it will be the same as the tableView background color
[backgroundView setBackgroundColor:[UIColor clearColor]];
//We can add anything to this text of images
[backgroundView addSubview:imageView];
//This is the important part
[self.tableView setBackgroundView:backgroundView];
现在要隐藏或查看背景视图,您需要在
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
中添加一些逻辑,例如:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Or wherever your data is
    if([self.data count]>0) {

        self.tableView.backgroundView.hidden=YES;
        return 1;

    }

    else {

        self.tableView.backgroundView.hidden=NO;


    }


    return 0;
}
只需检查有多少节,如果表中没有节,则显示视图。如果有分区,则将其隐藏

编辑,显然,如果您愿意,也可以在界面生成器中设置UIView。确保尺寸正确。使用以下内容加载if:

UIView *view = [[[NSBundle mainBundle]
        loadNibNamed:@"MyView"
        owner:self options:nil]
         firstObject];
您需要再次设置它:
[self.tableView setBackgroundView:view]