简体   繁体   中英

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). 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.

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 . Finally in order to set the frame of tableView height to 0 u can make use of didSet method of your data source

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

Write the setter to your array. If your array name is 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:

Step 1: Open your xib/storyboard which contains the tableView.

Step 2: Select the tableViewHeight constraint in xib/storyBoard

Step 3: Now control drag from height constraint to the viewController which owns the tableView

在此处输入图片说明

The logic of using setter of the array to decide whether to show tableView or not is the logic I wanted to convey. 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 :)

You can check for your array count & put your table rect to Zero as below

- (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. You can place where you want in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM