我在loaddescriptiondata方法中获得了正确的URL。 我的问题是在url请求后执行tableview,而不是connectiondidfinishloading。 为什么会这样?
这是我的代码:
解析operation.m :
-(void)loadDescriptiondata:(NSString*)itemId{
isDescription = TRUE;
[self initilizeData];
NSString *stringToAppend = @"?method=showMenuItemDesc&res_id=";
NSString *append2=@"&groupcat=individual&app=iphone";
NSString *newURLAsString = [NSString stringWithFormat:@"%@%@%@%@",URL,stringToAppend,itemId,append2];
NSURL *url = [NSURL URLWithString:newURLAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
NSLog(@"url shop desp=%@",url);
}
viewDidLoad中:
- (void)viewDidLoad{
parse = [[ParseOperation alloc]init];
categoryImageUrl =[[NSMutableArray alloc]init];
parse.delegate = self;
}
Mytableview :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LazyTableCell";
// add a placeholder cell while waiting on table data
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;
}
else {
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
iId=[ids objectAtIndex:indexPath.row];
NSLog(@".....id... %@",iId);
[parse loadDescriptiondata:(NSString *)iId];
NSString *urlString = [categoryImageUrl objectAtIndex:indexPath.row];
urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *url = [NSURL URLWithString:urlString];
[asyncImageView loadImageFromURL:url];
}
被修改
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
#pragma mark -
#pragma mark Process loan data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSLog(@"Connected");
// NSLog(@"respond string %@",responseString);
NSArray *latestLoans = [responseString JSONValue] ;
categoryData = [[CategoryData alloc] init];
for(int i=0;i<[latestLoans count];i++)
{
NSString* shortTitle13 = [loan objectForKey:@"photourl"];
spiceType = [loan objectForKey:@"spicinesstype"];
img =[loan objectForKey:@"photourl"];
[array2 addObject:shortTitle13];
if(isDescription == TRUE){
[spiceTypeArray addObject:spiceType];
[spiceArrayID addObject:spiceID];
}
}
if(isDescription == TRUE){
[self.delegate didFinishParsingImageUrl:array2];//phot
}
答案 0 :(得分:0)
我没有看过AsyncImageView,但是没有使用任何外部框架,我完成了我认为你要完成的工作,如下所述:http://dbrajkovic.wordpress.com/2012/01/08/load-images-asynchronously-in-a-uitableview-using-gcd-grand-central-dispatch/
<强>被修改强> 首先,让我们在这里进行一些错误检查。我每次创建NSURLConnection都会这样做:
if (urlConnection) {
// Create the NSMutableData to hold the received data.
responseData = [NSMutableData data];
} else {
// Inform the user that the connection failed.
}
答案 1 :(得分:0)
查看代码后,我只能假设您没有与urlConnection属性建立强连接。那就是“@property(非原子,强)NSURLConnection urlConnection”。有点晚了,但希望这有助于其他人..