2011-06-01 52 views
2

我想知道如何正确地释放单元格并修复内存泄漏。并且还询问如果数字单元格数目变高,内存泄漏是否会导致滚动崩溃?正确地从UITAbleView释放单元格

这是我的手机泄漏来自任何建议?

NSString *identifier = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

if (cell == nil) { 



} 


//add subtitle 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:@"cell"]; 




    /* 
    //Here it adds a nice shadow to the table view but will crash on rotation and send a 
    wierd dump to ApplistViewController !!!???? 
    tableView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
    tableView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); 
    tableView.layer.shadowRadius = 8.0f; 
    tableView.layer.shadowOpacity = 1.0f;  
    */ 


[cell.textLabel setNumberOfLines:1]; 
[cell.textLabel setText:[(Tweet*)[authors objectAtIndex:indexPath.row] author]]; 

[cell.detailTextLabel setText:[(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]]; 
[cell.detailTextLabel setNumberOfLines:10]; 
[cell.textLabel setTextColor:[UIColor darkGrayColor]]; 
[cell.textLabel setShadowColor:[UIColor whiteColor]]; 
[cell.textLabel setShadowOffset:CGSizeMake(0.5, 0.5)]; 
[cell.detailTextLabel setTextColor:[UIColor blackColor]]; 
//[cell.detailTextLabel setText:[(Tweet*)[retweetCount objectAtIndex:indexPath.row] 
reTweetCount]]; 
[cell.textLabel setUserInteractionEnabled:YES]; 
[cell.contentView setMultipleTouchEnabled:YES]; 
// cell.text = [[NSString alloc] initWithFormat:@"Cell :%i", indexPath.row];  





NSURL *url = [NSURL URLWithString:[(Tweet*)[avatarsURL 
objectAtIndex:indexPath.row]avatarURL]]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
// NSLog(@"http://a0.twimg.com/profile_images/1268205919/iDD_normal.png"); 
UIImage *tableImage = [[UIImage alloc] initWithData:data]; 
[cell.imageView setImage:tableImage]; 
cell.imageView.image = tableImage ; 
CGSize imageSize = CGSizeMake(45,45); 
UIGraphicsBeginImageContext(imageSize); 
CGRect imageRect = CGRectMake(0.2, 0.1, imageSize.width, imageSize.height); 
[tableImage drawInRect:imageRect]; 
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();  







//PlaceMent avatars! would like to use a if statment to place this image in the cell if 
before it retrives the avatar just like the method above for the profile! 

/* 
NSString *avatar = [[NSBundle mainBundle] pathForResource:@"avatar" ofType:@"png"]; 
UIImage *setAvatar = [[UIImage alloc] initWithContentsOfFile:avatar]; 

//now place avatar in cell 
cell.imageView.image = setAvatar; 
*/  

//add gradient to cell 
UIImage *gradient = [UIImage imageNamed:@"gradientcell2.png"]; 
UIImageView *cellimage = [[UIImageView alloc] initWithImage:gradient]; 
cellimage.contentMode = UIViewContentModeScaleToFill; 
cell.backgroundView = cellimage; 


    [cellimage release];  



UIImage *selectedGradient = [UIImage imageNamed:@"selectedcell.png"]; 

UIImageView *selectedCell = [[UIImageView alloc] initWithImage:selectedGradient]; 
selectedCell.contentMode = UIViewContentModeScaleToFill; 
cell.selectedBackgroundView = selectedCell;  

//subtitle if needed !! Also change UITableViewStlyle from default to Subtitle up 7 
lines 
// cell.detailTextLabel.text = @"subtile"; 



//yeah another warning within the tableview whats new =P 
[tableView setBackgroundColor:[UIColor clearColor]];  


return cell; 


[cell autorelease]; 
} 

ALL的泄漏是固定!!!!谢谢,但为什么当我添加一个计数整数时会崩溃?

有什么建议吗?

2011-06-01 18:24:55.663 ThemeCatcher[4641:207] *** Terminating app due to uncaught  
exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 16 
beyond bounds [0 .. 15]' 
*** Call stack at first throw: 
(
0 CoreFoundation      0x014dcbe9 __exceptionPreprocess + 185 
1 libobjc.A.dylib      0x016315c2 objc_exception_throw + 47 
2 CoreFoundation      0x014d26e5 -[__NSArrayM objectAtIndex:] + 
261 
3 ThemeCatcher      0x00025833 -[TwitterVeiwController 
tableView:cellForRowAtIndexPath:] + 531 
4 UIKit        0x007bb7fa - 
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634 
5 UIKit        0x007b177f - 
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75 
6 UIKit        0x007c6450 - 
[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561 
7 UIKit        0x007be538 -[UITableView layoutSubviews] + 
242 
8 QuartzCore       0x00460451 -[CALayer layoutSublayers] + 181 
9 QuartzCore       0x0046017c CALayerLayoutIfNeeded + 220 
10 QuartzCore       0x0045937c 
_ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310 
11 QuartzCore       0x004590d0 _ZN2CA11Transaction6commitEv + 
292 
12 QuartzCore       0x004897d5 
_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 
13 CoreFoundation      0x014bdfbb 
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27 
14 CoreFoundation      0x014530e7 __CFRunLoopDoObservers + 295 
15 CoreFoundation      0x0141bbd7 __CFRunLoopRun + 1575 
16 CoreFoundation      0x0141b240 CFRunLoopRunSpecific + 208 
17 CoreFoundation      0x0141b161 CFRunLoopRunInMode + 97 
18 GraphicsServices     0x01efa268 GSEventRunModal + 217 
19 GraphicsServices     0x01efa32d GSEventRun + 115 
20 UIKit        0x0075642e UIApplicationMain + 1160 
21 ThemeCatcher      0x000021c9 main + 121 
22 ThemeCatcher      0x00002145 start + 53 
23 ???         0x00000001 0x0 + 1 

) 

terminate called after throwing an instance of 'NSException' 
Current language: auto; currently objective-c 
(gdb) 

回答

0
  1. cell对象,这里需要自动释放 - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];这行需要在里面if (cell == nil) { //here }
  2. 你alloc-init tableImage但不释放它。
  3. selectedCell也未发布。
  4. 从方法返回后您是autorelease ing。这不会发生,如果是可重复使用的单元格,它会发送额外的release消息使应用程序崩溃。

在我的评论中似乎有更多的问题,我选择不包括。尝试使用Leaks应用程序检查内存泄漏。

+0

谢谢!!!我正在使用泄漏工具来发现它,我是如何知道它的。但是,当你说我知道该怎么做,并且现在在我的应用程序中有零泄漏,那要归功于你!我很感激!我希望他们没有什么我失踪.....当我添加一个计数整数,而不仅仅是返回计数我崩溃滚动....所以例如,如果我说count = 100;我会在滚动期间崩溃,但我从来没有通过说退货计数来崩溃; ummmmm ??? – FreeAppl3 2011-06-01 23:18:20

-1

当您在cellForRowAtIndex中编写以下代码时,只需autorelease即可。你的问题解决了。

static NSString *cellIdentifier = @"cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if(cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellSeparatorStyleSingleLine reuseIdentifier:cellIdentifier] autorelease]; 
} 
相关问题