我在实施UITableView
时遇到问题,其中每个单元格都有UIImageView
,UILabel
。我正在使用延迟加载,因为使用JSON从Web服务检索数据。
我有一个按钮,需要删除子视图(比如UIView
)并调整TableView的大小。
我的问题是,当我点击按钮时,UITableView的滚动变得很慢。当我分析我的应用程序时,它显示UITmageView中UIImageView的泄漏。
这是我在UITableView中的代码,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.color = [UIColor blueColor];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
if (lblTitle==nil){
lblTitle =[[UILabel alloc]init];
}
if (thumbnailImage==nil){
thumbnailImage = [[UIImageView alloc]init];
}
}
[cell addSubview:lblTitle];
[cell addSubview:thumbnailImage];
// set frames for table view cell
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone){
lblTitle.frame = CGRectMake(110, 0, 210, 100);
thumbnailImage.frame = CGRectMake(5, 5, 100, 90);
activityIndicator.frame = CGRectMake(40, 30, 35, 35);
lblTitle.font = [UIFont fontWithName:@"Papyrus" size:16.0f];
}else{
lblTitle.frame = CGRectMake(215, 0, 590, 150);
lblTitle.font = [UIFont fontWithName:@"Papyrus" size:25.0f];
thumbnailImage.frame = CGRectMake(5, 5, 200, 140);
activityIndicator.frame = CGRectMake(85, 45, 75, 75);
}
lblTitle.numberOfLines = 2;
lblTitle.textColor = [UIColor colorWithRed:105/255.0f green:205/255.0f blue:220/255.0f alpha:1.0f];
lblTitle.backgroundColor = [UIColor clearColor];
thumbnailImage.tag = indexPath.row;
// display data
lblTitle.text = [[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"title"];
//lazy loding of images
DataContainer *currentContainer;
if (profImageDataContainer.count!=0)
currentContainer= [profImageDataContainer objectAtIndex:indexPath.row];
if(currentContainer.cellImage)
thumbnailImage.image = currentContainer.cellImage;
else{
//checking whether thumbnail is null
if (![[[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"image"] isKindOfClass:[UIImage class]])
{
if ([self checkNetworkStatus:nil]){
if(![[[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"thumbnail"] isEqualToString:@""] && ([[[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"thumbnail"] rangeOfString:@".jpg"].location!=NSNotFound || [[[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"thumbnail"] rangeOfString:@".jpeg"].location!=NSNotFound || [[[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"thumbnail"] rangeOfString:@".png"].location!=NSNotFound))
{
activityIndicator.tag = indexPath.row;
// Start it animating and add it to the view
[activityIndicator startAnimating];
[cell addSubview:activityIndicator];
NSString *urlstr = [[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"thumbnail"];
urlstr = [urlstr stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *imgURL = [NSURL URLWithString:urlstr];
thumbnailImage.image=[UIImage imageNamed:@"albumsPlaceHolder.png"];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:imgURL,[NSString stringWithFormat:@"%d", indexPath.row],activityIndicator,thumbnailImage, nil];
[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:array];
}else
{
thumbnailImage.image=[UIImage imageNamed:@"albumsPlaceHolder.png"];
}
}
else
{
thumbnailImage.image=[UIImage imageNamed:@"albumsPlaceHolder.png"];
}
}
else
{
thumbnailImage.image=[[rssNewsArray objectAtIndex:indexPath.row] objectForKey:@"image"];
}
}
thumbnailImage=nil;
lblTitle=nil;
activityIndicator=nil;
return cell;
}
和按钮操作
- (IBAction)actCloseTicker:(id)sender
{
[self.ticker pause];
self.ticker=nil;
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setBool:NO forKey:@"ticker"];
[def synchronize];
def=nil;
[self.tickerView removeFromSuperview];
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
self.tblRSSNews.frame = CGRectMake(0, 0, 320, self.view.frame.size.height);
}
else
{
self.tblRSSNews.frame = CGRectMake(0, 0, 768, self.view.frame.size.height);
}
}
提前致谢。
答案 0 :(得分:1)
根据发布的代码,您的tableView没有重用创建的单元格,因为您正在使用reuseIdentifier:nil
分配单元格,这意味着每次需要显示新单元格时,它将分配一个新单元格并添加标签和imageView的新实例,可能是缓慢的可能性。您可以尝试更改以下代码:
UILabel *lblTitle = nil;
UIImageView *thumbnailImage = nil;
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId ];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
if (lblTitle==nil){
lblTitle =[[UILabel alloc]init];
lblTitle.tag = 1000;
}
if (thumbnailImage==nil){
thumbnailImage = [[UIImageView alloc]init];
thumbnailImage.tag = 1001;
}
[cell addSubview:lblTitle];
[cell addSubview:thumbnailImage];
}
else{
lblTitle = (UILabel*)[cell viewWithTag:1000];
thumbnailImage = (UIImageView*)[cell viewWithTag:1001];
}