【问题标题】:iOS6 UIPickerView memory leak issue.iOS6 UIPickerView 内存泄漏问题。
【发布时间】:2012-10-24 08:27:00
【问题描述】:

我遇到了这个内存泄漏:

[UIPickerTableViewTitleCell initWithStyle:resuableIdentifier]; 

NSConcentrateMutableAttributedString.

问题是我没有实现这个委托。在实现这个之后,内存泄漏就消失了。这些信息可能对其他人有帮助,因为我花了 16 个小时才弄清楚这个问题。

// Do something with the selected row.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

// Get the text of the row.
NSString *rowItem = [NSString stringWithFormat:@"     %@",[machineData objectAtIndex:row]];

// Create and init a new UILabel.
// We must set our label's width equal to our picker's width.
// We'll give the default height in each row.
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];

// Make the text color red.
[lblRow setTextColor: [UIColor blackColor]];
[lblRow setFont:[UIFont boldSystemFontOfSize:20]];

// Center the text.
[lblRow setTextAlignment:UITextAlignmentLeft];

// Add the text.
[lblRow setText:rowItem];

// Clear the background color to avoid problems with the display.
[lblRow setBackgroundColor:[UIColor clearColor]];

// Return the label.
return lblRow;
}

【问题讨论】:

  • 我不知道为什么,但这是真的。我花了几个小时寻找那个内存泄漏。找到这篇文章后,我将 pickerView:titleForRow:forComponent: 替换为 pickerView:viewForRow:forComponent:reusingView: ,内存泄漏消失了!非常感谢

标签: iphone objective-c memory-leaks xcode4.5 uipickerview


【解决方案1】:

感谢您的信息。被这个泄漏弄糊涂了。只有几个cmets:

  • 可能 lblRow 应该被自动释放:return [lblRow autorelease];

  • [pickerView rowSizeForComponent:component] 可用于获取新标签的大小。

【讨论】:

  • 我正在使用 ARC,这就是为什么这让生活变得轻松一些。其次,我对 ARC 的了解是它首先释放了那些参考为 nil 的人。例如像 lblRow = nil;但不知道“返回”的最佳做法是什么。
【解决方案2】:

我在弹出框中使用了 IUIPicker,每次我关闭弹出框时都会出现内存泄漏。我也在使用 ARC,所以我解决这个问题的最简单方法是在卸载时设置 UIPickerView = nil。以下似乎已经成功了。

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.pickerView = nil;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多