我有一个使用reuseCellIdentifier 并包含图像的UITableView(使用UIImageView+AFNetworking.h 异步下载);正如所料,滚动一段时间后,当表格变长时,它会变得迟缓。我还将从 Internet 下载的每个单元格的数据存储在一个NSMutableArray *_collection
.
我的问题是,我用下面的方法完全刷新表格后,滚动还是很慢。让它再次加载流畅的唯一方法是退出应用程序并重新打开它。我不明白为什么重新加载后滚动仍然缓慢......我正在使用ARC并且我做了泄漏配置文件并且当我重新加载表格时看不到任何内容..
-(void)refreshTable:(id)sender{
[_collection removeAllObjects];
_collection = nil;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:_baseURL]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:_path parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation ...etc...
更进一步,我还尝试在刷新时删除 tableview,如下所示,但这也无济于事......
-(void)refreshTable:(id)sender{
[_tableView removeFromSuperView];
_tableView = nil;
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[_tableView setDelegate:self];
[_tableView setDataSource:self];
[_tableView setRowHeight:kRowHeight];
[_tableView setBackgroundColor:[UIColor clearColor]];
[_tableView setSeparatorColor:[UIColor clearColor]];
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.view addSubview:_tableView];
[_collection removeAllObjects];
_collection = nil;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:_baseURL]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:_path parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation ...etc...
刷新表格后滚动仍然缓慢的原因可能是什么?谢谢!
编辑
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)[theTableView dequeueReusableCellWithIdentifier:kCustomCellId];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = (CustomCell*)[topLevelObjects objectAtIndex:0];
}
[self populate:cell atIndexPath:indexPath];
[self loadImageForCell:cell];
return cell;
}
- (void) populate:(UITableViewCell*)customCell atIndexPath:(NSIndexPath*)indexPath
{
CustomCell *cell = (CustomCell*)customCell;
if (indexPath.row % 2) {
[cell.background setImage:[UIImage imageNamed:@"CellBgYellow"]];
} else {
[cell.background setImage:[UIImage imageNamed:@"CellBgOrange"]];
}
DataObject* thisDataObject = [_collection objectAtIndex:indexPath.row];
cell.dataObject = thisDataObject;
cell.titleLabel.text = [thisDataObject objectForKey:kCellTitle];
cell.timestampLabel.text = [thisDataObject objectForKey:kCellTimestamp];
}
- (void) loadImageForCell:(CustomCell*)cell
{
[cell.profilePic setImage:nil];
NSString* profilePicURL = [cell.dataObject objectForKey:kCellProfilePicURL];
NSString* URL = [NSString stringWithFormat:@"%@%@", kBaseURL, profilePicURL];
NSMutableURLRequest *profilePicRequest = [NSMutableURLRequest requestWithURL:[[NSURL alloc] initWithString:URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[profilePicRequest setHTTPShouldHandleCookies:NO];
[cell.profilePic setImageWithURLRequest:profilePicRequest
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"loadimageforcell error (%d): %@",error.code,error.localizedDescription);
}];
[cell.painting setImage:nil];
NSString* paintingURL = [cell.dataObject objectForKey:kCellPaintingURL];
URL = [NSString stringWithFormat:@"%@%@", kBaseURL, paintingURL];
NSMutableURLRequest *paintingRequest = [NSMutableURLRequest requestWithURL:[[NSURL alloc] initWithString:URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[paintingRequest setHTTPShouldHandleCookies:NO];
[cell.spinner startAnimating];
[cell.painting setImageWithURLRequest:paintingRequest
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
[cell.spinner stopAnimating];
[cell.spinner removeFromSuperview];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"loadimageforcell error (%d): %@",error.code,error.localizedDescription);
[cell.spinner stopAnimating];
[cell.spinner removeFromSuperview];
}];
}