我使用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];
}
顺便说一句:** retainCount **是无用的。别叫它。 – bbum 2011-05-09 15:17:03