我正在使用UITableView。如果没有网络连接,则viewDidload中会抛出异常。我的viewDidLoad函数是:
@try {
NSLog(@"Request URL = %@",URLString);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:URLString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *tableData = [NSJSONSerialization JSONObjectWithData:response
options:0
error:&jsonParsingError];
// Grab whole data with data field in JSON
// responseArray = [tableData objectForKey:@"data"];
responseArray = [[NSMutableArray alloc]initWithArray:[tableData objectForKey:@"data"]];
for(int i = 0; i < responseArray.count; i++)
{
NSArray * tempArray = responseArray[i];
responseArray[i] = [tempArray mutableCopy];
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(280.0, 0.0, 40.0, 40.0)];
[btn setImage:[UIImage imageNamed:@"sort_icon.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barbutton;
}
@catch (NSException *exception)
{
exceptionOccured = YES;
NSLog(@"Exception Ocurred");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error in connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
在cellForRowAtIndexPath中我这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
@try {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
tempDict = [responseArray objectAtIndex:indexPath.row];
return cell;
}
@catch (NSException *exception)
{
NSLog(@"Error in CEll Create");
NSLog(@"Draw Alert");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error in connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
在AlertViewDelegate函数中我正在做
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.navigationController popViewControllerAnimated:YES];
}
现在问题是,只要有异常并且重新抛出异常并显示错误
,它就不会显示警报Thread 1: EXC_BAD_ACCESS(code=2, address=0x2)
任何帮助将不胜感激......
答案 0 :(得分:1)
您应该避免在代码中抛出异常。
首先,您可以使用Reachability Class来确定是否有可用的互联网连接。
我明确建议使用NSURLConnectionDelegate
协议进行网址连接。因此,您可以使用更好的异步编程风格。
答案 1 :(得分:0)
问题出在其他地方。调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
时,表示系统需要重新加载tableView。因此,您不应检查该函数内的数据。当您输入此功能时,您应确保您的数据正常。
您需要做的是首先检查特定功能中的数据:
-(void)CheckData(NSArray *responseArray) {
@try {
//Retrieve your data & check if its valid
self.dataArray = responseArray;
[self.tableView reloadData];
}
@catch (NSException *exception)
{
NSLog(@"Error in check Data");
NSLog(@"Draw Alert");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error in connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
然后,实现您的数据源委托:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
//TODO: Init your cell using your self.dataArray content
return cell;
}
答案 2 :(得分:0)
我建议导入这个SystemConfiguration
框架并使用相同的框架。这将检查网络连接是否可用。以下是简单的代码: -
#import <SystemConfiguration/SystemConfiguration.h>
-(void)yourMethod
{
SCNetworkConnectionFlags flags = 0;
if (yourhostname && [yourhostname length] > 0)
{
flags = 0;
BOOL found = NO;
SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [yourhostname UTF8String]);
if (reachabilityRef)
{
found = SCNetworkReachabilityGetFlags(reachabilityRef, &flags)
&& (flags & kSCNetworkFlagsReachable)
&& !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachabilityRef);
reachabilityRef = NULL;
}
if (found)
{
NSLog(@"Connection available");
}
else
{
NSLog(@"Connection not available");
}
}