xcode 当 UITableView 为空时,显示一个 UIImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8124428/
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
When an UITableView is empty, show an UIImage
提问by awDemo
This is related to another question of mine which wasn't answered in a helpful way (message when a UITableView is empty).
这与我的另一个问题有关,该问题没有以有用的方式回答(UITableView 为空时的消息)。
I'm trying to show an UIImage graphic that says You haven't saved any bookmarksover an UITableView
when it's empty. I have NSNotification set-up so that when bookmarks are added or deleted, a message is sent so that the UITableView can be updated.
我试图表现出UIImage的图形,说你没有保存任何书签上的UITableView
时候它是空的。我有 NSNotification 设置,以便在添加或删除书签时,会发送一条消息,以便可以更新 UITableView。
I've been trying to do it with this code. Why won't this work?
我一直在尝试用这段代码来做到这一点。为什么这行不通?
- (void)bookmarksChanged:(NSNotification*)notification
{
[self.tableView reloadData];
UIImageView* emptyBookmarks = [[UIImageView alloc] initWithFrame:CGRectMake(75, 100, 160, 57)];
emptyBookmarks.alpha = 1;
emptyBookmarks.image = [UIImage imageNamed:@"emptyBookmark.png"];
[self.view addSubview:emptyBookmarks];
[emptyBookmarks release];
if ([self.dataModel bookmarksCount] == 0)
{
emptyBookmarks.alpha = 1;
}
else
{
emptyBookmarks.alpha = 0;
}
}
I'm probably approaching this the wrong way... But if salvageable, what am I doing wrong?
我可能以错误的方式接近这个......但如果可以挽救,我做错了什么?
When I initially have an empty bookmarks tableview, there's no image displayed. After I add a bookmark and then delete it, the image shows. Grrh.
当我最初有一个空的书签 tableview 时,没有显示图像。添加书签然后将其删除后,图像显示。咕噜。
采纳答案by Norman G
Well first off if you were going to do it that way, you would need to reload the tableView after updating the image or model etc. and not before. But you are probably making things more complicated than they need to be! Why not just check to see if the data for section 0 and indexPath.row 0 are empty and if so in cellForRowAtIndexPath display a text message accordingly.
首先,如果您打算这样做,则需要在更新图像或模型等之后而不是之前重新加载 tableView。但是你可能让事情变得比他们需要的更复杂!为什么不直接检查第 0 部分和 indexPath.row 0 的数据是否为空,如果是,则在 cellForRowAtIndexPath 中相应地显示一条文本消息。
// First make sure there is always one row returned even if the dataModel is empty.
// 首先确保即使 dataModel 为空也总是返回一行。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numRows = 0;
if ([self.dataModel lastObject]) {
// Return the number of rows in the section.
numRows = [self.dataModel count]; // etc.
}
if (numRows < 1) numRows = 1;
return numRows;
}
// Then display the data if there is some, otherwise a message if empty.
// 如果有则显示数据,否则如果为空则显示消息。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if ([self.dataModel lastObject]) {
// setup the cell the normal way here.
} else { // the datasource is empty - print a message
cell.textLabel.text = nil;
cell.detailTextLabel.text = NSLocalizedString(@"You haven't saved any bookmarks", @"");
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.7];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
回答by Ray Fix
Another way (and IMO the correct way) to do this is to manipulate the backgroundView property on the UITableView.
另一种方法(和 IMO 的正确方法)是操作 UITableView 上的 backgroundView 属性。
While making a single cell with a custom image cell would certainly works, I think it overly complicates the logic of your UITableViewController's data source. It feels like a kludge.
虽然使用自定义图像单元格制作单个单元格肯定会起作用,但我认为它使 UITableViewController 数据源的逻辑过于复杂。感觉就像一团糟。
According to UITableView documentation:
根据 UITableView 文档:
A table view's background view is automatically resized to match the
size of the table view. This view is placed as a subview of the table
view behind all cells , header views, and footer views.
Assigning an opaque view to this property obscures the background color
set on the table view itself.
While you probably don't want to just set it to your UIImageView, it is very easy to make a UIView that contains the UIImageView that you want.
虽然您可能不想将它设置为您的 UIImageView,但制作包含您想要的 UIImageView 的 UIView 非常容易。
回答by relower
Are you sure [self.dataModel bookmarksCount]
is equal to 0 ?
你确定[self.dataModel bookmarksCount]
等于 0 吗?
回答by MadhavanRP
While I agree that you are probably going about this the wrong way,
虽然我同意你可能以错误的方式去做这件事,
your image is allocated and added in your bookmark changed, your notification does not trigger when there are no bookmarks initially. Hence you don't see the image. Call the bookmar changed when your table view inits or appears.
您的图像已分配并添加到您的书签中已更改,当最初没有书签时,您的通知不会触发。因此你看不到图像。当您的表视图初始化或出现时调用书签已更改。
回答by Davido
Probably the best way to achieve this is to perform a check in your numberOfRowsInSection method to return 1 if your data source is empty. Then in cellForRowAtIndexPath check if your data source is empty and if it is, create a custom cell that contains whatever you want. In heightForRowAtIndexPath you need to return your custom cell height if your datasource is empty, but only if you want the cell larger than the default. At least that is how I would approach it.
可能实现此目的的最佳方法是检查 numberOfRowsInSection 方法以在数据源为空时返回 1。然后在 cellForRowAtIndexPath 中检查您的数据源是否为空,如果是,则创建一个包含您想要的任何内容的自定义单元格。在 heightForRowAtIndexPath 中,如果数据源为空,则需要返回自定义单元格高度,但前提是您希望单元格大于默认值。至少我会这样处理它。
回答by Nik Burns
when bookmarks count is nil add one to your row method:
当书签计数为零时,向您的行方法添加一个:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int c;
c = bookmarks.count;
if(c == 0){
c = 1;
}
return c;
}
and then the same check again in your cellforrowatindexpath.
然后在您的 cellforrowatindexpath 中再次进行相同的检查。
回答by Jeremy Kates
Another thing to be aware of in this situation is that if you're using core data and you're datasource is feeding off an entity, you will want to make sure your model matches. You can get some weird side-effect behavior in certain situations. This is especially true if you allow editing and core data has an empty model but you're tableview is still showing a cell.
在这种情况下要注意的另一件事是,如果您正在使用核心数据并且您的数据源正在从一个实体中获取数据,您将需要确保您的模型匹配。在某些情况下,您可能会出现一些奇怪的副作用行为。如果您允许编辑并且核心数据有一个空模型,但您的 tableview 仍然显示一个单元格,则尤其如此。