IPhone UITableView Background When Table Is Empty

I want to show a wallpaper when my UITableView is empty. Currently, I have tried adding a UIImageView to my view controller, which contains a table, but Xcode does not allow this.

Is there a good way to do this?

+6
source share
3 answers

You can either add an image view over the table view or change the background view of the table view.

// Check if table view has any cells int sections = [self.tableView numberOfSections]; BOOL hasRows = NO; for (int i = 0; i < sections; i++) { BOOL sectionHasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO; if (sectionHasRows) { hasRows = YES; break; } } if (sections == 0 || hasRows == NO) { UIImage *image = [UIImage imageNamed:@"test.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; // Add image view on top of table view [self.tableView addSubview:imageView]; // Set the background view of the table view self.tableView.backgroundView = imageView; } 
+15
source

UITableView has a backgroundView property. Set this property to a UIImageView containing the background image.

+4
source

In your method numberOfRowsInSection :

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if data.count > 0 { return data.count } else{ let image = UIImage(named: "Nature") let noDataImage = UIImageView(image: image) noDataImage.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: tableView.frame.height) tableView.backgroundView = noDataImage tableView.separatorStyle = .none return 0 } } 

numberOfSections must be greater than 0

0
source

Source: https://habr.com/ru/post/906344/


All Articles