gpt4 book ai didi

iphone - UITableView 滚动缓慢,内存泄漏问题

转载 作者:行者123 更新时间:2023-11-29 11:13:00 24 4
gpt4 key购买 nike

我有一个 UITableView 和一个 UITableCell 子类。每个表格单元格都有两个 scrollviews,每个 ScrollView 旋转一个动态数量的标签,这些标签是在我的表格单元格实现中创建的。我的滚动性能很差,我相信这是内存泄漏造成的。我正在引用此 stackoverflow 正确答案来解决我的问题:cellForRowAtIndexPath 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];

}

最佳答案

您在使用 ARC 吗?如果没有,你应该自动释放到

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

单元格对象被重用,每次更改内容时不需要重新添加 subview 。只需更改数据并根据需要更新标签框。

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

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

关于iphone - UITableView 滚动缓慢,内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10669139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com