开发者

How to add subview ontop of UITableView?

I have a UITableView that gets populated with data if there is a live network connection. When there is no connection I would like to add a view to the c开发者_运维知识库urrent view that will say No Internet Connection. Similar to the Photos app when there are no synced photos. I am not sure how I can accomplish this.


It's not exactly what you want, but you could have an "Empty Data" table view, with enough cells too allow scrolling, and the first cell containing a message like "No Network Connection".

The alternative is to place the table view in another (parent) view and swap that (child) table view for an empty view when there's no network.


Generally Apple suggests UIAlertView for this. The examples in the Developer center usually have UIAlertViews, simply because Apple deems the lack of network availability in network-requiring apps as an important enough thing to alert the user to.

I use the following (keeping the copyright intact of course)

in UIAlertView+Helper.h:

//
//  UIAlertViewHelper.h
//  CocoaHelpers
//
//  Created by Shaun Harrison on 10/16/08.
//  Copyright 2008 enormego. All rights reserved.
//

#import <UIKit/UIKit.h>

/*
 * Convenience method to throw a quick alert to the user
 */
void UIAlertViewQuick(NSString* title, NSString* message, NSString* dismissButtonTitle);

@interface UIAlertView (Helper)

@end

in UIAlertView+Helper.m:

//
//  UIAlertViewHelper.m
//  CocoaHelpers
//
//  Created by Shaun Harrison on 10/16/08.
//  Copyright 2008 enormego. All rights reserved.
//

#import "UIAlertView+Helper.h"

void UIAlertViewQuick(NSString* title, NSString* message, NSString* dismissButtonTitle) {
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil 
                                          cancelButtonTitle:dismissButtonTitle
                                          otherButtonTitles:nil
                          ];
    [alert show];
    [alert autorelease];
}


@implementation UIAlertView (Helper)

@end

then in your own app -- using Reachability, of course. For example:

- (void) updateInterfaceWithReachability: (Reachability*) curReach;

UIAlertViewQuick(@"You're offline!", @"Sorry, it looks like you lost your Internet connection. Please reconnect and try again.", @"OK");                    

I use this helper in many of my apps cos it makes it quite easy to present an alertView to the user.

Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜