xcode 如果 UITableview 为空显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14345062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:31:37  来源:igfitidea点击:

If UITableview is empty show image

iphoneobjective-cxcodeuitableview

提问by user1883396

I am looking for 3 hours now on Google how to remove the tableview and show an image when the tableview is empty (have no more rows). Does someone know this? I know it's possible, because I saw it on many apps.

我现在在谷歌上寻找 3 个小时如何删除 tableview 并在 tableview 为空时显示图像(没有更多行)。有人知道吗?我知道这是可能的,因为我在很多应用程序上都看到过。

What I could find was:

我能找到的是:

// Check if table view has any cells
int sections = [self.tableView numberOfSections];
BOOL hasRows = NO;
for (int i = 0; i < sections; i++)
    hasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO;

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;
}

where to put this?

把这个放在哪里?

Thanks!

谢谢!

回答by RyanG

If your using Storyboard just put your view behind your UITableView

如果你使用 Storyboard 只是把你的观点放在你的后面 UITableView

If your array of data is empty when creating it, simply hide your UITableViewto show the "empty table" view behind it.

如果您的数据数组在创建时为空,只需隐藏您的数据即可UITableView在其后面显示“空表”视图。

[tableView setHidden:YES];

回答by lmnbeyond

Please refer to: http://www.unknownerror.org/Problem/index/905493327/how-do-i-display-a-placeholder-image-when-my-uitableview-has-no-data-yet/

请参考:http: //www.unknownerror.org/Problem/index/905493327/how-do-i-display-a-placeholder-image-when-my-uitableview-has-no-data-yet/

Thanks to Cocoanut and Thomas:

感谢 Cocoanut 和 Thomas:

@interface MyTableViewController : UITableViewController
{
      BOOL hasAppeared;
      BOOL scrollWasEnabled;
      UIView *emptyOverlay;
}

- (void) reloadData;
- (void) checkEmpty;

@end

@implementation MyTableViewController

- (void)viewWillAppear:(BOOL)animated
{
   [self reloadData];
   [super viewWillAppear: animated];
}

- (void)viewDidAppear:(BOOL)animated
{
   hasAppeared = YES;

   [super viewDidAppear: animated];

   [self checkEmpty];
}

- (void)viewDidUnload
{
   if (emptyOverlay)
   {
      self.tableView.scrollEnabled = scrollWasEnabled;
      [emptyOverlay removeFromSuperview];
      emptyOverlay = nil;
   }
}

- (UIView *)makeEmptyOverlayView
{
    UIView *emptyView = [[[NSBundle mainBundle] loadNibNamed:@"myEmptyView" owner:self options:nil] objectAtIndex:0];
    return emptyView;
}

- (void) reloadData
{
   [self.tableView reloadData];

   if (hasAppeared &&
       [self respondsToSelector: @selector(makeEmptyOverlayView)])
      [self checkEmpty];
}

- (void) checkEmpty
{
   BOOL isEmpty = YES;

   id<UITableViewDataSource> src = self.tableView.dataSource;
   NSInteger sections(1);
   if ([src respondsToSelector: @selector(numberOfSectionsInTableView:)])
      sections = [src numberOfSectionsInTableView: self.tableView];
   for (int i = 0; i < sections; ++i)
   {
      NSInteger rows = [src tableView: self.tableView numberOfRowsInSection: i];
      if (rows)
         isEmpty = NO;
   }

   if (!isEmpty != !emptyOverlay)
   {
      if (isEmpty)
      {
         scrollWasEnabled = self.tableView.scrollEnabled;
         self.tableView.scrollEnabled = NO;
         emptyOverlay = [self makeEmptyOverlayView];
         [self.tableView addSubview: emptyOverlay];
      }
      else
      {
         self.tableView.scrollEnabled = scrollWasEnabled;
         [emptyOverlay removeFromSuperview];
         emptyOverlay = nil;
      }
   }
   else if (isEmpty)
   {
      // Make sure it is still above all siblings.
      [emptyOverlay removeFromSuperview];
      [self.tableView addSubview: emptyOverlay];
   }
}

@end

回答by Ramy Al Zuhouri

UITableViewis a (non direct) subclass of UIView, so what you want to do is easy.

UITableView是 的(非直接)子类UIView,所以你想要做的很容易。

Say that you have this table view as subview of your view controller's view. This case you just create another view with the same frame as the table view, then you remove your table view from the superview, and add the newly created view as subview of your view controller's view. So simply:

假设您将此表视图作为视图控制器视图的子视图。在这种情况下,您只需创建另一个与表视图具有相同框架的视图,然后从超级视图中删除表视图,并将新创建的视图添加为视图控制器视图的子视图。很简单:

UIImageView* view= [[UIImageView alloc] initWithImage: yourImage];
view.frame= tableView.frame;
[tableView removeFromSuperview];
[self.view addSubview: view];

回答by Alejandro La Rosa

Put it on - (void)viewDidAppear. Good luck ;)

穿上它 - (void)viewDidAppear。祝你好运 ;)