Iphone iOS6 UIPickerView内存泄漏问题。
我得到了这个内存泄漏:Iphone iOS6 UIPickerView内存泄漏问题。,iphone,objective-c,memory-leaks,xcode4.5,uipickerview,Iphone,Objective C,Memory Leaks,Xcode4.5,Uipickerview,我得到了这个内存泄漏: [UIPickerTableViewTitleCell initWithStyle:resuableIdentifier]; 及 问题是我没有实现这个委托。在实现这一点之后,内存泄漏就消失了。也许这些信息对其他人有用,因为我花了16个小时才弄明白这个问题 // Do something with the selected row. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteg
[UIPickerTableViewTitleCell initWithStyle:resuableIdentifier];
及
问题是我没有实现这个委托。在实现这一点之后,内存泄漏就消失了。也许这些信息对其他人有用,因为我花了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;
}
谢谢你的信息。他被这次泄密弄糊涂了。只有几条评论:
- 可能lblRow应该自动删除:
return[lblRow autorelease]代码>
可用于获取新标签的大小[pickerView rowSizeForComponent:component]
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.pickerView = nil;
}
我使用的是ARC,所以这就是为什么它让生活变得有点简单。其次,我对ARC的了解是,它首先释放那些引用为零的对象。例如,像lblRow=nil;但我不知道“退货”的最佳做法是什么。我不知道为什么,但这是真的。我花了几个小时寻找那个内存泄漏。找到这篇文章后,我将pickerView:titleForRow:forComponent:替换为pickerView:viewForRow:forComponent:reusingView:,内存泄漏消失了!谢谢
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.pickerView = nil;
}