Iphone NSURLConnection,tableview GUI挂起

Iphone NSURLConnection,tableview GUI挂起,iphone,ios4,Iphone,Ios4,我正在使用NSData DATA with CONTENTS OFURL方法在tableview中显示图像,但当我滚动tableview GUI时,图像被挂起。因此在论坛上搜索后,我发现我可以尝试使用NSUrlConnection方法。所以我尝试了,但我不能成功地实现它 请在下面找到我的代码 请帮我弄清楚我怎样才能正确地做这件事 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UIT

我正在使用NSData DATA with CONTENTS OFURL方法在tableview中显示图像,但当我滚动tableview GUI时,图像被挂起。因此在论坛上搜索后,我发现我可以尝试使用NSUrlConnection方法。所以我尝试了,但我不能成功地实现它

请在下面找到我的代码

请帮我弄清楚我怎样才能正确地做这件事

// 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];
}

dataWithContentsOfURL
是一个同步网络请求。这意味着,在调用代码的地方,它将等到请求完成后再转到下一条指令。同步网络不好。真糟糕。它只有在测试中才真正起作用

您应该做的是启动对这些映像的异步请求。上面的代码速度非常慢的原因是,
tableView
每次都向其数据源代理请求
cellforrowatinexpath:
;您的代码同步触发网络请求-这意味着在图像的网络请求完成之前,单元不会被返回


相反,您应该做的是在请求
tableView
时异步加载所有图像。在它们返回时使用标记来识别它们。这在你所做的事情中并不容易;因此,您可能希望在显示
tableView
时启动所有NSURLConnections,为
numberOfSectionsInTableView
返回
0
,直到连接完成,然后在
tableView
上调用
reloadData
(并使
numberOfSectionsInTableView
现在返回要显示的正确行数).

dataWithContentsOfURL
是一个同步网络请求。这意味着在调用代码的地方,它将等待请求完成,然后再转到下一条指令。同步网络是不好的。非常不好。它只在测试中真正起作用

您应该做的是启动对这些图像的异步请求。上面的代码速度惊人的原因是,
tableView
每次向其数据源代理请求
cellforrowatinexpath:
;您的代码同步启动一个网络请求-这意味着该单元格不会被重新加载直到完成对映像的网络请求为止

相反,您应该做的是在请求
tableView
时异步加载所有图像。这在图像返回时使用标记来识别它们。这在您所做的整个上下文中并不容易;因此,当显示
tableView
时,您可能希望启动所有NSURLConnections,return
0
对于
numberOfSectionsInTableView
直到连接完成,然后在连接完成后调用
tableView
上的
reloadData
(并使
numberOfSectionsInTableView
现在返回要显示的正确行数)。

开始使用ASI库: 越快越好。

开始使用ASI库:
越快越好。

我想这可能会解决你的问题…

我想这可能会解决你的问题…

顺便说一句:重新计算没有用。不要叫它。顺便说一句:重新计算没有用。不要叫它。