2011-06-01 167 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]; 
}