- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在实现 UITableView
时遇到问题,其中每个单元格都有一个 UIImageView
、UILabel
。我正在使用延迟加载,因为数据是使用 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/
标题看起来很长,但我想做的事情很简单。 我有几个相同的按钮排成一排,并将它们的标签设置为 0...n。单击其中任何一个(例如第二个)都会弹出一个弹出 View ,其中有几个代表不同选项的按钮(A、B、
我正在尝试添加动画以在点击按钮时淡出按钮。我现在使用的代码只是等待几秒钟,然后几乎立即淡出按钮。我想知道如何在点击按钮后立即开始淡出按钮并使淡入淡出持续大约一秒钟? 这是我目前使用的代码: UIVie
我正在尝试将属性字符串设置为按钮。 这个函数在swift上的定义是这样的 func setAttributedTitle(_ title: NSAttributedString!,
我的 UI 上有多个按钮,我想根据不同类型的点击执行不同的功能, 单击 双击 长按 对我来说,单击一下很容易,一个连接了所有四个按钮的 IBAction,但对于其他类型的点击,我被卡住了, 我知道我需
我测试了大量样本,但没有一个对我有用。这是我的代码: override func viewDidLoad() { super.viewDidLoad() let button = UI
我正在创建一个这样的按钮: let authorizationButton = UIButton() let authorizationButtonHeight = 50.0 let
我不久前才开始编码,所以对于任何明显的错误提前道歉。 我使用 Storyboard创建了两个按钮。我知道您可以像 this solution 一样更改发件人按钮的标题声明了,但我仍然不确定如何在单击第
想象一下用于 Apple TV 的维基百科之类的应用程序,其中包含一段文本,其中包含有关给定主题的信息。文本中的某些单词或短语可能是您可以阅读的其他可用主题。因此,您需要将这些单词/短语制作成可聚焦和
我正在调整一些视觉变化,并注意到当我尝试设置 UIButton 的背景颜色时,它仅设置实际按钮外部的颜色(而不是按钮本身内部的颜色) UIButton* button = [UIButton butt
这里有一个问题要问你: 我有一个 UIButton,我们将其称为 bottomButton。我有另一个 UIButton,我们将其称为 topButton。我想要做的是将 topButton 放在 b
我观察到以下情况: 通过设置 UIButton 的标题颜色就外观而言,UIMenuItems在 UIMenuController的UITextView得到相同的颜色。 applicationDidFi
应用程序从 API 产品选项加载,例如电子商务应用程序的颜色和尺寸。现在,我有点陷入代码困境。 如果选择了一个按钮,让我们说出它的颜色,则应取消选择另一个按钮,因为一次只能选择一种颜色。请看一下下面的
我想从左向右滑入一个 UIButton(屏幕截图中的灰色)并将其置于屏幕中央。虽然已经有一个 UIButton(屏幕截图中的橙色)居中,但应将此按钮推到右侧,并且始终与滑入的 UIButton 保持
如何在 UIButton 之上添加 UIButton? 这样一来,如果我移动父 UIButton,顶部的 UIButton 也会移动。 最佳答案 UIButton *testButton = [UIB
.因此,它将通过使用数组和 random() 触摸 UIButton 与 IBAction 来显示按钮上随机 1-6 个骰子图像的图像,以从图像数组中选择以在图像背景上显示 #import "View
我正在尝试以编程方式创建一个 UIButton。我有一个名为“addCash”的按钮(已在界面生成器中创建),点击此按钮后我希望另一个按钮动态出现。当在 viewDidLoad 中完成时,此按钮工作正
我有一个 UIButton 对象,它的标题是在运行时确定的。标题可能是多行的,所以我想增加 UIButton 对象的高度以匹配其标题标签的高度。我不想创建一个 UIButton 子类,因为我读到这不是
在 Storyboard上我有两个 UIButton。在我的 UIViewController 子类中,我有一个用于此按钮的 IBAction 方法。其中一个按钮有图像但没有标题,另一个按钮有标题但没
我想要一个背面有另一个按钮的按钮。当您按下按钮时,它应该水平翻转并显示另一个按钮。 Apple 在 ipod-App 中做到了这一点。当您听到一首歌曲时,您可以按下导航栏右侧的按钮,然后 View 会
我有一个自定义字段,允许用户在 UITextField 中输入文本。 UITextField 的左侧是一个 UIButton。像这样的东西: 输入您的姓名:[姓名输入栏] 在关于 UIButton 中
我是一名优秀的程序员,十分优秀!