ios - 当 UITableViewController 为空时显示 "what' s this? 消息

标签 ios cocoa-touch uitableview

我的应用包含多个 UITableViewController,它们不一定在所有情况下都有内容。例如,如果用户没有任何草稿,则草稿屏幕为空。

在这种情况下,我想显示一条简短的消息来解释屏幕的用途,例如内置照片应用中的屏幕:

An iOS navigation bar over a blank screen containing the outlines of two photos, followed by the caption "No Photos or Videos", then the text "You can take photos or videos using the camera, sync photos or videos onto your iPhone using iTunes, or add photos and videos from other albums and events."

在屏幕上获得描述性 View 的最佳方式是什么?我不能直接将 UIViewController 子类化,因为我依赖于一些专门绑定(bind)到 UITableViewController 的 iOS 6 功能,所以据我所知,我必须在 UITableView 中显示此 View 。有什么建议吗?

最佳答案

子类 UITableViewController,然后在 -viewDidAppear: 或其他类似的适当位置,检查表格中的单元格数是否为零。如果是这样,请添加此叠加层;如果没有,请确保移除覆盖层。示例代码如下:

@interface MyTableViewController : UITableViewController
...
@property (nonatomic, weak) UIImageView *informativeOverlayImageView;
...
@end

@implementation MyTableViewController

...

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

    // Just for an example - you'll have your own logic for determining if there will be zero rows.
    if (self.myDataModel.items.count == 0 &&
        !self.informativeOverlayImageView.superview)
    {
        if (!self.informativeOverlayImageView)
        {
            self.informativeOverlayImageView = [[UIImageView alloc] initwithImage:[UIImage imageNamed:@"someImageName"]];
            [self.informativeOverlayImageView sizeToFit];
        }
        [self.view addSubview:self.informativeOverlayImageView];
    }
    else if (self.myDataModel.items.count > 0 &&
            self.informativeOverlayImageView.superview)
    {
        [self.informativeOverlayImageView removeFromSuperview];
        [self.tableView reloadData]; // Add animations to taste.
    }
}

...

@end

希望这对您有所帮助!

关于ios - 当 UITableViewController 为空时显示 "what' s this? 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12277862/

相关文章:

ios - 如何从 URL 在 TableView 中添加图像

ios - 自动关闭弹出窗口,如 Tweetbot iOS 应用程序

objective-c - 使用 leftChild 和 rightChild 实现二叉树是不好的做法吗?

ios - 就地调整 UIImage 的大小

ios - 带有 UITableView 示例的 MagicalRecord

iOS:无法在 UITableViewCell 中使用 Activity Indicator

ios - 允许在 iFrame 内缩放,但不允许在 iOS 页面上缩放

ios - CGRect 调用中的 Swift 额外参数 "width"

iphone - 从相机胶卷中选择的视频的 URL

ios - 如何将 UIButtons 添加到我的 TableViewCell 中?