我正在根据用户从表格视图中选择的内容从在线服务器(在本例中为Google协作平台)加载图像视图(表格视图正在从在线加载的plist中填充)。该代码已经工作,但我正在尝试为无法加载的图像添加警报视图。这是我的表视图控制器。
#import "TableViewController.h"
#import "MapViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize trailList;
@synthesize trailView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString: @"https://sites.google.com/site/majorprojectgjk/Trails.plist"];
trailList = [[NSMutableArray alloc] initWithContentsOfURL: url];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [trailList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"trailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellid];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellid];
}
cell.textLabel.text = [trailList objectAtIndex: indexPath.row];
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: @"pushView"])
{
NSIndexPath *indexPath = [self.trailView indexPathForSelectedRow];
MapViewController *mvc = segue.destinationViewController;
mvc.subjects = [trailList objectAtIndex: indexPath.row];
}
}
这将是我的地图视图控制器代码。
#import "MapViewController.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize map;
@synthesize subjects;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: (BOOL)animated];
NSString *site = @"https://sites.google.com/site/majorprojectgjk/";
NSLog(@"Display: %@", subjects);
NSString *mapString = @"Map";
NSString *png = @".png";
//NSString *url = [NSString stringWithFormat: @"%@%@%@%@",site, subjects, mapString, png];
@try
{
NSString *space = subjects;
if ([space rangeOfString:@" "].location == NSNotFound)
{
NSString *url = [NSString stringWithFormat: @"%@%@%@%@",site, subjects, mapString, png];
map.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [[NSURL alloc] initWithString: url]]];
NSLog(@"URL: %@", url);
}
else
{
NSLog(@"string contains space");
NSString *removeSpace = [subjects stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *url = [NSString stringWithFormat: @"%@%@%@%@",site, removeSpace, mapString, png];
map.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [[NSURL alloc] initWithString: url]]];
NSLog(@"URL %@", url);
}
}
@catch (NSException *ex) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"The map could not load!" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
正如我之前所说,这些代码完全正常。图像从URL完全下载,并且数据也从表视图中解析。我想知道如何使用@try @catch块来提醒用户UIImageView是空的(当URL无效且地图尚未加载到服务器时会发生这种情况。)
答案 0 :(得分:0)
`enter code here`@catch(NSException *ex)
{
NSString *str=[ex copy];
//write code for alert
}