从UITAbleView正确释放单元格

时间:2011-06-01 22:48:32

标签: 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];
}

所有泄漏都是固定的!!!!谢谢,但为什么在添加计数整数时会崩溃?

有什么建议吗?

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) 

2 个答案:

答案 0 :(得分:0)

  1. cell对象需要在此自动发布 - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];,此行必须位于if ( cell == nil ) { //here }内。
  2. 你alloc-init tableImage但不发布它。
  3. selectedCell也未发布。
  4. 从方法返回后,您autorelease。这不会发生,如果是可重复使用的单元格,它会发送一条额外的release消息,导致您的应用崩溃。
  5. 您的评论中似乎有更多问题我选择不包括在内。尝试使用Leaks应用程序检查内存泄漏。

答案 1 :(得分:0)

当你在cellForRowAtIndex中编写下面的代码时,只需自动释放它。你的问题解决了。

static NSString *cellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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