Iphone 如果UITableview为空,则显示图像
我正在谷歌上寻找3个小时如何删除tableview并在tableview为空(没有更多行)时显示图像。有人知道吗?我知道这是可能的,因为我在很多应用程序上都看到了 我能找到的是:Iphone 如果UITableview为空,则显示图像,iphone,objective-c,xcode,uitableview,Iphone,Objective C,Xcode,Uitableview,我正在谷歌上寻找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 = ([
// 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;
}
//检查表视图是否有单元格
int sections=[self.tableView numberOfSections];
BOOL hasRows=否;
对于(int i=0;i0)?是:不是;
如果(节==0 | | hasRows==NO)
{
UIImage*image=[UIImage ImageName:@“test.png”];
UIImageView*imageView=[[UIImageView alloc]initWithImage:image];
//在表视图顶部添加图像视图
[self.tableView addSubview:imageView];
//设置表格视图的背景视图
self.tableView.backgroundView=imageView;
}
把这个放在哪里
谢谢
UITableView
是UIView
的一个(非直接)子类,因此您想做的事情很简单
假设此表视图是视图控制器视图的子视图。在这种情况下,您只需创建另一个与表视图具有相同框架的视图,然后从superview中删除表视图,并将新创建的视图添加为视图控制器视图的子视图。简单地说:
UIImageView* view= [[UIImageView alloc] initWithImage: yourImage];
view.frame= tableView.frame;
[tableView removeFromSuperview];
[self.view addSubview: view];
如果您使用的是故事板,只需将视图放在
UITableView
如果数据数组在创建时为空,只需隐藏UITableView
即可在其后面显示“空表”视图
[tableView setHidden:YES];
请参阅:
感谢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
@接口MyTableViewController:UITableViewController
{
布尔出现了;
BOOL已启用;
UIView*emptyOverlay;
}
-(作废)重新加载数据;
-(void)checkEmpty;
@结束
@MyTableViewController的实现
-(无效)视图将显示:(BOOL)动画
{
[自重新加载数据];
[超级视图将显示:动画];
}
-(无效)视图显示:(BOOL)动画
{
是的;
[超级视图显示:动画];
[自检为空];
}
-(无效)视图卸载
{
如果(清空溢出)
{
self.tableView.scrollEnabled=scrollWasEnabled;
[emptyOverlay从SuperView中移除];
emptyOverlay=零;
}
}
-(UIView*)makeEmptyOverlayView
{
UIView*emptyView=[[NSBundle mainBundle]loadNibNamed:@“myEmptyView”所有者:自选项:nil]对象索引:0];
返回空视图;
}
-(void)重新加载数据
{
[self.tableView重载数据];
如果(他)出现了&&
[自响应选择器:@selector(makeEmptyOverlayView)])
[自检为空];
}
-(void)checkEmpty
{
BOOL isEmpty=是;
id src=self.tableView.dataSource;
NSInteger段(1);
if([src respondsToSelector:@selector(numberOfSectionsInTableView:)]))
sections=[src numberOfSectionsInTableView:self.tableView];
对于(int i=0;i
将其置于-(void)视图显示。祝你好运;) 可能重复:@user1883396编辑问题以添加该代码,它太长,无法在一条注释中显示。我应该将其放在-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath中{
?将它放在为表创建数组时使用的任何位置—在向数组中添加对象之后,在重新加载表之前,检查该数组的计数是否为0,如果不是仅隐藏表,则