UITableView滚动速度慢,内存泄漏问题 [英] UITableView scrolling slow, memory leak issue

查看:151
本文介绍了UITableView滚动速度慢,内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView 和一个 UITableCell 子类。每个表格单元格都有两个滚动视图,每个滚动视图都会旋转动态数量的标签,这些标签是在我的表格单元格实现中创建的。我的滚动性能很差,我相信它来自内存泄漏。我正在引用此 stackoverflow正确答案来解决我的问题 cellForRowAtIndexPath内存管理

I have a UITableView and a UITableCell subclass. Each table cell has two scrollviews that each rotate a dynamic amount of labels, which are created in my table cell implementation. I have poor scrolling performance and I believe it is from memory leaks. I'm referencing this stackoverflow correct answer to fix my problem: cellForRowAtIndexPath memory management.

我无法弄清楚如何调整代码,所以每次创建标签时都不必分配内存。

I can't figure out how to tweak my code so I don't have to allocate memory every time I create labels.

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Custom Cell";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


NSDictionary *dictionary = [parseDataArray objectAtIndex: indexPath.row];

NSArray *popularLinkTitleArray = [dictionary objectForKey:@"popularLinkTitleArray"];
NSString *soundCloudLink = [dictionary objectForKey:@"soundCloudLink"];
NSArray *soundCloudTrackTitleArray = [dictionary objectForKey:@"soundCloudTrackTitleArray"];


cell.artistNameLabel.text = artistName;

[cell createPopularLinkLabels: popularLinkTitleArray];

return cell;
}

CustomCell.m

- (void)layoutScrollLabelsForPopularLinkScrollView: (float)arrayCount
{
UIView *view = nil;
NSArray *subviews = [popularLinkScrollView subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[UILabel class]] && view.tag >= 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

[popularLinkScrollView setContentSize:CGSizeMake((arrayCount * kScrollObjWidth),    [popularLinkScrollView bounds].size.height)];

}

-(void) createPopularLinkLabels:(NSArray *) popularLinkTitleArray
{

popularLinkScrollView.clipsToBounds = YES;

kScrollObjHeight = popularLinkScrollView.frame.size.height;
kScrollObjWidth = popularLinkScrollView.frame.size.width;

for (UIView* subView in popularLinkScrollView.subviews)
    [subView removeFromSuperview];

NSUInteger i;
    for (i = 0; i < popularLinkTitleArray.count; i++)
    {
        NSString *string = [NSString stringWithFormat:@"%@", [popularLinkTitleArray objectAtIndex: i]];
        UILabel *label = [[UILabel alloc] init];
        label.text = [NSString stringWithFormat:@"%@", string];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 5;
        [label setFont:[UIFont fontWithName:@"Calibri" size:18]];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = label.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        label.frame = rect;
        label.tag = i;  // tag our images for later use when we place them in serial fashion
        [popularLinkScrollView addSubview:label];
    }

[self layoutScrollLabelsForPopularLinkScrollView:popularLinkTitleArray.count];

}


推荐答案

第一个你可以做的是尝试在createPopularLinkLabels中重用UILabel。现在,您只是在添加更多内容之前删除它们。

The first thing you could do is try to reuse UILabels in createPopularLinkLabels. Right now, you are just removing them before adding more.

但如果流行链接的数量在所有单元格中都不相同,那么你将不得不删除剩余的链接或隐藏它们并且将无法避免内存分配。

But if the number of popular links is not the same in all the cells, then you will have to remove the remaining ones or hide them and there will be no way to avoid memory allocations.

更好的解决方案是在滚动视图中插入自定义UIView并手动绘制所有文本,而不是创建大量的子视图。

A better solution would be to insert a custom UIView inside your scroll view and draw all the texts manually instead of creating tons of subviews.

希望这会有所帮助,
Thomas

Hope this helps, Thomas

这篇关于UITableView滚动速度慢,内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆