2012-05-19 36 views
1

我有一個UITableView和一個UITableCell子類。每個表格單元有兩個scrollviews,每個旋轉一個動態數量的標籤,這是在我的表格單元實現中創建的。我的滾動性能不佳,我相信它來自內存泄漏。我參考了這個stackoverflow correct answer to fix my problemcellForRowAtIndexPath memory managementUITableView滾動緩慢,內存泄漏問題

我不知道如何調整我的代碼,所以我不必每次創建標籤時都分配內存。

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

} 

回答

0

你可以做的第一件事就是嘗試重用createPopularLinkLabels UILabels。現在,在添加更多內容之前,您只需刪除它們。

但是,如果流行鏈接的數量在所有單元格中都不相同,那麼您將不得不刪除剩餘的鏈接或隱藏它們,並且將無法避免內存分配。

更好的解決方案是在您的滾動視圖中插入自定義的UIView並手動繪製所有文本,而不是創建大量的子視圖。

希望這有助於 托馬斯

+0

你可以提供更多關於如何插入UIView和繪製文本的細節?我是iOS新手。謝謝! – mnort9

+0

爲每個單元格的滾動視圖繪製視圖文本會比使用UILabels更高效? – mnort9

+0

嘿 - 對不起,我錯過了你的回覆...看看:「Subclassing UITableViewCell」https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html – sarfata

2

是否使用ARC?如果沒有,你應該自動釋放到

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

Cell對象被重用,你不需要每次更改內容時重新添加子視圖。只需更改數據並根據需要更新標籤框。

如果您有明顯不同的單元格,請考慮使用單獨的自定義單元格類。您需要爲reuseIdentifier使用單獨的值。

也可以考慮不要創建自定義單元格,而只是向標準UITableViewCell添加元素。 Read this about styling a cell

+0

我認爲你不需要if(cell == nil)。因爲單元格會在滾動結束時自動釋放。所以每次細胞都會產生。 – Anish