【问题标题】:If UITableview is empty show image如果 UITableview 为空,则显示图像
【发布时间】:2013-01-15 19:08:49
【问题描述】:

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

我能找到的是:

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

把这个放在哪里?

谢谢!

【问题讨论】:

标签: iphone objective-c xcode uitableview


【解决方案1】:

如果您使用 Storyboard,只需将您的视图放在您的 UITableView 后面

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

[tableView setHidden:YES];

【讨论】:

  • 我应该把它放在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 中?
  • 把它放在你创建你的数组的任何地方,你为你的表使用 - 在你添加对象之后和重新加载你的表之前,检查该数组的计数是否为 0,如果它比然后隐藏表格
【解决方案2】:

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

感谢 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

【讨论】:

    【解决方案3】:

    UITableViewUIView 的(非直接)子类,所以你想做的很简单。

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-28
        相关资源
        最近更新 更多