这与我的另一个问题有关,该问题没有以有用的方式回答(UITableView 为空时的消息)。
我试图显示一个 UIImage 图形,当它为空时,该图形显示您尚未在 UITableView
上保存任何书签。我设置了 NSNotification,以便在添加或删除书签时发送一条消息,以便更新 UITableView。
我一直在尝试用这段代码来做到这一点。为什么这不起作用?
- (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;
}
}
我可能以错误的方式处理这个问题......但如果可以挽救,我做错了什么?
当我最初有一个空书签表格 View 时,没有显示图像。添加书签然后将其删除后,将显示该图像。呃。
最佳答案
另一种方法(在我看来是正确的方法)是操作 UITableView 上的 backgroundView 属性。
虽然使用自定义图像单元格创建单个单元格肯定可行,但我认为它使 UITableViewController 数据源的逻辑过于复杂。感觉就像是一个拼凑。
根据 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.
虽然您可能不想将其设置为 UIImageView,但制作包含所需 UIImageView 的 UIView 非常容易。
关于iphone - 当 UITableView 为空时,显示 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8124428/