使用ASIHTTPRequest启动异步并更新UITableView但使用EXC_BAD_ACCESS失败

时间:2014-06-06 09:15:59

标签: ios objective-c uitableview asynchronous asihttprequest

viewDidLoad

中的

{
[super viewDidLoad];
postjson = [[PostJson alloc] init];
[postjson startpost];



_table = [[UITableView  alloc] init];
FOOTPRINT;

_table.delegate = self;
_table.dataSource =self;
[self.view addSubview:_table];
}
startpost

中的

-(NSMutableArray*)startpost:(NSString *)title category_id:(NSString *)category_id limits:(NSNumber *)limits blog_id:(NSString*)blog_id pull_direction:(NSString *)pull_dirction
{


    __block NSMutableArray *dataToBeShown =[[NSMutableArray alloc] init];
    __block NSMutableDictionary *resultsDictionary;

    NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:title, @"title",category_id,@"category_id",limits,@"limits",blog_id,@"blog_id", pull_dirction,@"pull_direction",nil];
    if ([NSJSONSerialization isValidJSONObject:userDictionary])
    {

        NSError *error;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDictionary options:NSJSONWritingPrettyPrinted error: &error];
        NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];


        NSURL *url = [NSURL URLWithString:[[PropertyPlist getValue:@"RootURL"] stringByAppendingString:@"/rest/blog/search"]];

        ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:url];
        __weak ASIHTTPRequest* request = _request;
        [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
        [request addRequestHeader:@"Accept" value:@"application/json"];

    [request setRequestMethod:@"POST"];
    [request setPostBody:tempJsonData];

    [request setDelegate:self];
    [request setCompletionBlock:^{
        NSString *response = [request responseString];
        NSLog(@"Test:%@",response);
        NSData* jsonData = [response dataUsingEncoding:NSUTF8StringEncoding];
        FOOTPRINT;
        resultsDictionary = [jsonData objectFromJSONData];
        NSArray *dataArray = [resultsDictionary objectForKey:@"data"];
        NSLog(@"即将进入FOR循环");
        int i =0;
        for ( NSDictionary *dict in dataArray){

            NSMutableArray *tempArray =[[NSMutableArray alloc] init];
            [tempArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"id"]];//取出ID
            [tempArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"thumb"]];//取出缩略图地址
            [tempArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"title"]];//取出title
            [tempArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"auto_id"]];//取出auto_id
            [dataToBeShown addObject:tempArray];
            tempArray=nil;
            i++;
        }


        NSLog(@"跳出FOR循环");

    }];

    [request setFailedBlock:^{
        NSError *error1 = [request error];
        NSLog(@"ERROR IS %@",error1);
    }];
    [request startAsynchronous];


    [ASIHTTPRequest setDefaultUserAgentString:kUserAgentString];


}



return dataToBeShown;

 METHOD_END;
}
cellForRowAtIndexPath

中的

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString *indentifier=@"indentifier";
RecommedTableViewCell *cell =(RecommedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:indentifier];
if (cell == NULL)
{

    cell=[[RecommedTableViewCell alloc]init];    }
NSInteger row=[indexPath row];



cell.titleLabel.text =[[dataToBeShown objectAtIndex:row] objectAtIndex:2];

[cell.image setImageWithURL:[[dataToBeShown objectAtIndex:row] objectAtIndex:1] ];


return cell;

}

一旦应用程序启动EXC_BAD_ACCESS,它就会崩溃。当我提交时

cell.titleLabel.text =[[dataToBeShown objectAtIndex:row] objectAtIndex:2];

[cell.image setImageWithURL:[[dataToBeShown objectAtIndex:row] objectAtIndex:1] ];

,而不是崩溃。所以我发现dataToBeShown为空。我该怎么做?

1 个答案:

答案 0 :(得分:0)

请在分配前检查阵列

如果(行

    [cell.image setImageWithURL:[[dataToBeShown objectAtIndex:row] objectAtIndex:1] ];
 }

或者在numberOfRowsInSection

中进行检查
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataToBeShown.count;
}

这将删除EXC_BAD_ACCESS。