2011-05-09 53 views
0

我使用NSData的dataWithContentsOfURL方法显示在tableview中的图像,但是当我滚动tableview中GUI搜索了论坛我发现我可以用NSURLConnection的方法尝试后得到hanged.so。所以我尝试过,但我不能成功实施它。NSURLConnection的,实现代码如下GUI挂起

请在下面找到我的代码...

好心帮我,我怎么能正确地完成它..

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DataIdentifier"] autorelease]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:249.0/255.0 blue:230.0/255.0 alpha:2.0]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     profileName = [appDelegate.arrCommunityUserList objectAtIndex:indexPath.row]; 

     NSString *imgName = [profileName.user_image stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

     NSString *strValue = [NSString stringWithFormat:@"%d", profileName.userID]; 

     if (tableView == myTableView) 
     { 
      cellRectangle = CGRectMake(15, 2, 75, 75); 

      NSString *myurl = [NSString stringWithFormat: @"%@pics/photos/%@/%@",ConstantImgURL, strValue,imgName]; 

      NSURL *url = [NSURL URLWithString: myurl]; 

      imageView = [[UIImageView alloc] initWithFrame: cellRectangle]; 

      NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:myurl]]; 
      [NSURLConnection connectionWithRequest:request delegate:self]; 

      // create the connection with the request 
      // and start loading the data 
      NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest: 
              request delegate:self]; 

      if (theConnection) 
      { 
       receivedData = [[NSMutableData data] retain]; 

      } 

      [cell.contentView addSubview:imageView]; 

     } 


    } 

return cell;  
} 

// did receive response 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
//-------------------------------------------------------------------------------------------------- 
{ 
    NSLog(@"Received response: %@", response); 
} 

// get recieved data 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
//---------------------------------------------------------------------------------- 
{ 
    // NSLog(@"Connection received data, retain count: %d", [connection retainCount]); 

    [receivedData appendData:data]; 

} 

// finished loading 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
//------------------------------------------------------------------- 
{ 
    // Set appIcon and clear temporary data/image 
    UIImage *image = [[UIImage alloc] initWithData:receivedData]; 
    imageView.image = image; 



} 

// connection failed with error 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)connError 
//--------------------------------------------------------------------------------------- 
{ 
    // NSLog(@"Error receiving response: %@", connError); 
    [connection release]; 
    [receivedData release]; 
} 
+0

顺便说一句:** retainCount **是无用的。别叫它。 – bbum 2011-05-09 15:17:03

回答

3

dataWithContentsOfURL是同步的网络请求。这意味着,在你的代码被调用的地方,它会等到请求结束后再继续下一条指令。同步网络是。特别糟糕。它只能在测试中起作用。

,你应该做的事情,是发射了这些图像异步请求。之所以上面的代码是缓慢的窘况是,该tableView要求其DataSource委托cellForRowAtIndexPath:每一次;您的代码会同步触发网络请求 - 这意味着在完成对图像的网络请求之前,单元格不会被返回。

相反,你应该做的是无论是在请求异步加载所有图像tableViewHere's a good example它使用标签来标识它们返回时的标识。在你所做的事情的整个背景中,这并不容易。所以当你显示tableView时,你可能想要启动所有的NSURLConnections,在numberOfSectionsInTableView之前返回0,直到连接完成,然后在tableView完成时调用reloadData(并且使numberOfSectionsInTableView返回正确的行数以显示)。

+0

同意....去寻求异步请求 – 2011-05-09 05:52:07