1

とサブクラスがUITableViewあります。UITableCell各テーブル セルにはscrollviews、テーブル セルの実装で作成された動的な量のラベルをそれぞれローテーションする 2 つのセルがあります。スクロールのパフォーマンスが低く、メモリリークが原因だと思います。私はこれを参照していますstackoverflow correct answer to fix my problem: cellForRowAtIndexPath メモリ管理

コードを微調整する方法がわからないので、ラベルを作成するたびにメモリを割り当てる必要がありません。

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

}
4

2 に答える 2

2

ARCを使用していますか?そうでない場合は、自動リリースする必要があります

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

セル オブジェクトは再利用されるため、内容を変更するたびにサブビューを再度追加する必要はありません。必要に応じて、データを変更してラベル フレームを更新するだけです。

明らかに異なるセルがある場合は、別のカスタム セル クラスを使用することを検討してください。reuseIdentifier には別の値を使用する必要があります。

また、カスタム セルを作成するのではなく、標準の UITableViewCell に要素を追加することも検討してください。 セルのスタイリングについては、こちらをお読みください

于 2012-05-19T22:17:05.293 に答える
0

最初にできることは、createPopularLinkLabels で UILabels を再利用することです。現時点では、さらに追加する前にそれらを削除しています。

ただし、人気のあるリンクの数がすべてのセルで同じでない場合は、残りのリンクを削除するか非表示にする必要があり、メモリ割り当てを回避する方法はありません。

より良い解決策は、大量のサブビューを作成する代わりに、スクロール ビュー内にカスタム UIView を挿入し、すべてのテキストを手動で描画することです。

これが役に立てば幸いです、トーマス

于 2012-05-19T22:16:51.410 に答える