UITableView滚动速度慢,内存泄漏问题

时间:2012-05-19 21:57:10

标签: iphone objective-c ios uitableview

我有一个UITableView和一个UITableCell子类。每个表格单元格都有两个scrollviews,每个表格都会旋转动态数量的标签,这些标签是在我的表格单元格实现中创建的。我的滚动性能很差,我相信它来自内存泄漏。我引用了这个stackoverflow correct answer to fix my problemcellForRowAtIndexPath memory management

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

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];

}

2 个答案:

答案 0 :(得分:2)

你在使用ARC吗?如果没有,你应该自动发布到

cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]

重复使用单元格对象,每次更改内容时都不需要重新添加子视图。只需更改数据并根据需要更新标签框架。

如果您有明显不同的单元格,请考虑使用单独的自定义单元格类。您需要为reuseIdentifier使用单独的值。

还要考虑不创建自定义单元格,而只是添加元素到标准UITableViewCell。 Read this about styling a cell

答案 1 :(得分:0)

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

但是如果所有单元格中流行链接的数量不同,那么您必须删除剩余的链接或隐藏它们,否则将无法避免内存分配。

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

希望这有帮助, 托马斯