[英]Objective-c remove table view when no data present to show
Currently in my app, when there is no data to show in my table view it shows a very tall white space and I would like that gone. 当前在我的应用程序中,当表格视图中没有数据可显示时,它显示出很高的空白,我希望该空白消失。
I need a way to programatically change the height to 0 or hide the view entirely from the layout flow (so it doesn't affect other UI components while there is no data). 我需要一种以编程方式将高度更改为0或完全从布局流程中隐藏视图的方法(因此,在没有数据的情况下,它不会影响其他UI组件)。 Seems no matter what approach I have taken that I have this ugly tall white space. 似乎无论我采取什么方法,我都拥有这个丑陋的白色空白。 I can only assume its because I have a height constraint on the table to be 500. I have tried to remove this programatically with no luck and just need some help. 我只能假设它是因为我在桌子上的高度限制为500。我试图以编程方式删除此列表没有运气,只需要一些帮助。
My code: 我的代码:
if([theData count] <= 0) {
CGRect frame = self.ScrollView.frame;
frame.size.height = 0;
self.TableView.frame = frame;
}
I have also tried: 我也尝试过:
if([theData count] <= 0) {
self.TableView = nil
}
and a number of other silly things not worth mentioning. 还有其他一些不值得一提的愚蠢的事情。 Can anyone give me a hand here? 有人可以帮我吗? I mostly just want the entire table gone when there is no data. 我主要只是希望没有数据时整个表都消失了。
As you have already mentioned that you have a height constraint on TableView which is set to 500. Create a IBOutlet
of tableView's height constraint lets say tableViewHeightConstraint
. 正如您已经提到的那样,TableView的高度限制设置为500。创建tableView的高度限制的IBOutlet
可以说tableViewHeightConstraint
。 Finally in order to set the frame of tableView height to 0 u can make use of didSet
method of your data source 最后,为了将tableView height的框架设置为0,您可以使用数据源的didSet
方法
var theData : [whatever_your_data_type] {
didSet {
if theData.count == 0 {
self.tableViewHeightConstraint.constant = 0
self.tableView.layoutIfNeeded()
}
else {
if self.tableViewHeightConstraint.constant == 0 {
self.tableViewHeightConstraint.constant = 500
self.tableView.layoutIfNeeded()
}
}
}
}
Code in Objective-C Objective-C中的代码
Write the setter to your array. 将设置器写入数组。 If your array name is theData, 如果您的阵列名称是theData,
-(void)setTheData:(NSMutableArray *)theData {
if(self.theData.count == 0) {
self.tableViewHeightConstraint.constant = 0;
[self.tableView layoutIfNeeded];
}
else {
if(self.tableViewHeightConstraint.constant == 0) {
self.tableViewHeightConstraint.constant = 500;
[self.tableView layoutIfNeeded];
}
}
}
EDIT 2: 编辑2:
Step 1: Open your xib/storyboard which contains the tableView. 第1步:打开包含tableView的xib / storyboard。
Step 2: Select the tableViewHeight constraint in xib/storyBoard 步骤2:在xib / storyBoard中选择tableViewHeight约束
Step 3: Now control drag from height constraint to the viewController which owns the tableView 步骤3:现在将控件从高度限制拖动到拥有tableView的viewController中
The logic of using setter of the array to decide whether to show tableView or not is the logic I wanted to convey. 我想传达的逻辑是使用数组的setter决定是否显示tableView的逻辑。 You can use this logic to hide the tableView/set the frame to 0/set the height constraint to 0 or whatever you wanna use :) 您可以使用此逻辑来隐藏tableView /将框架设置为0 /将高度约束设置为0或您想使用的任何方法:)
You can check for your array count & put your table rect to Zero as below 您可以检查数组计数并将表rect设置为零,如下所示
- (void)viewDidLoad {
[super viewDidLoad];
self.array =[NSMutableArray array];
// Do any additional setup after loading the view, typically from a nib.
if (self.array.count ==0)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.myTable.frame = CGRectZero;
});
}
}
I have written the code in viewDidLoad. 我已经在viewDidLoad中编写了代码。 You can place where you want in your code. 您可以在代码中放置所需的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.