【问题标题】:Objective C: UITableView Scrolling becomes Laggy目标 C:UITableView 滚动变得滞后
【发布时间】:2011-07-04 20:42:07
【问题描述】:

我有一个 UITableView 在滚动时变得非常迟钝。我的代码sn-p如下:

在我的“cell setupCell”中,我添加了一些自定义项到单元格内容视图中

  • 1 X UIImageView

  • 3 X UILabel

  • 3 个 X 按钮(UIControl 的子类)

我的初始测试似乎指向导致延迟的按钮。我在按钮类中有一些检查

我不确定延迟的确切原因是什么,因为这些检查似乎是非常简单和基本的操作。有人对通常导致这种滞后的原因有什么建议吗?

编辑:添加更多代码

我只想补充一点,当我第一次启动应用程序时滚动很流畅,在我多次上下滚动表格后它变得迟钝和缓慢。我的代码如下:

if (cell == nil)
{
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
    //Add like button
    UICustomButton *likeButton = [[UICustomButton alloc]init];
    likeButton.tag = 7;

    //Add comment button
    UICustomButton *commentButton = [[UICustomButton alloc]init];
    commentButton.tag = 8;

    //Add answer too button
    UICustomButton *answerButton = [[UICustomButton alloc]init];
    answerButton.tag = 9;

    [self.contentView addSubview:likeButton];
    [self.contentView addSubview:commentButton];
    [self.contentView addSubview:answerButton];

    [likeButton release];
    [commentButton release];
    [answerButton release];
}


//Set like button
UICustomButton *thisLikeButton = (UICustomButton *)[self.contentView viewWithTag:7];
[thisLikeButton setButtonWithAnswer:self.answerForCell buttonType:@"like" navcon:self.navcon andFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, totalCommentLabelHeight + CELL_SPACING*4, 45, CELL_BUTTON_HEIGHT)];
thisLikeButton.imageView.image = [UIImage imageNamed:@"heart.png"];


//Set comment button
UICustomButton *thisCommentButton = (UICustomButton *)[self.contentView viewWithTag:8];
 [thisCommentButton setButtonWithAnswer:self.answerForCell buttonType:@"comment" navcon:self.navcon andFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN + 45 + 5, totalCommentLabelHeight + CELL_SPACING*4, 80, CELL_BUTTON_HEIGHT)];
thisCommentButton.imageView.image = [UIImage imageNamed:@"chat.png"];

//Set answer button
UICustomButton *thisAnswerButton = (UICustomButton *)[self.contentView viewWithTag:9];    
[thisAnswerButton setButtonWithAnswer:self.answerForCell buttonType:@"join in" navcon:self.navcon andFrame:CGRectMake(1.5*CELL_TEXT_LEFT_MARGIN + 45 + 5 + 80 + 5, totalCommentLabelHeight + CELL_SPACING*4, 60, CELL_BUTTON_HEIGHT)];
thisAnswerButton.imageView.image = [UIImage imageNamed:@"beer-mug_white.png"];

我检查了仪器,它们似乎没有表明这部分代码有太多泄漏

在一系列滚动后,任何人都可以建议可能是什么原因导致延迟?

【问题讨论】:

  • 'loadContentAndHeight' 有什么作用?
  • @mindnoise,我在每个单元格中的内容各不相同,因此“loadContentAndHeight”方法计算内容所需的单元格高度。奇怪的是滚动一开始很流畅,但在我上下滚动多次后开始变慢并变得迟钝。

标签: objective-c ios uitableview scroll lag


【解决方案1】:

在 Instruments 中运行它,当你滚动时看看它在哪里花费时间。

【讨论】:

  • 我检查了仪器,他们似乎没有告诉我上面的代码有泄漏。
  • 你不应该寻找泄漏,而是你的时间花在哪里。
  • @NSResponder,我是否使用分配工具来检查?看看计数?
【解决方案2】:

检查原因是否是您设置cornerRadius时(评论它)。另外,我在您的代码中看不到,但如果您还为每行的元素添加阴影,可能是一个原因。

【讨论】:

  • 感谢您的建议。不幸的是,这对性能没有帮助。我没有给我的行添加阴影。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 2023-04-06
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多