gpt4 book ai didi

ios - 单击 UIButton 时 UITableView 卡住

转载 作者:行者123 更新时间:2023-11-29 03:21:55 26 4
gpt4 key购买 nike

我在实现 UITableView 时遇到问题,其中每个单元格都有一个 UIImageViewUILabel。我正在使用延迟加载,因为数据是使用 JSON 从 Web 服务检索的。

我有一个按钮需要删除一个 subview (比如 UIView)并调整 TableView 的大小。

我的问题是,当我单击按钮时,UITableView 的滚动变得非常缓慢。当我分析我的应用程序时,它显示 UITableView 中的 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);
}
}

提前致谢。

最佳答案

根据发布的代码,您的 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];
}

关于ios - 单击 UIButton 时 UITableView 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com