我已經成功創建了一個UITableView/UITableViewCell,它可以上下滑動並調整高度以顯示選項。看看這是什麼意思,我製作了一個視頻here。代碼如下:這段代碼中有沒有內存泄漏?
- (void)swipe:(UISwipeGestureRecognizer *)recognizer direction:(UISwipeGestureRecognizerDirection)direction
{
if (recognizer && recognizer.state == UIGestureRecognizerStateEnded)
{
// Get the table view cell where the swipe occured
CGPoint location = [recognizer locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:location];
MyCell* cell = (MyCell *) [self.tableView cellForRowAtIndexPath:indexPath];
[self.tableView beginUpdates];
//removing the options view at the other cell before adding a new one
if (global != nil && global.row != indexPath.row){
[sideSwipeView removeFromSuperview];
sideSwipeView = nil;
}
//options already exist, we need to remove it
if (sideSwipeView != nil){
[sideSwipeView removeFromSuperview];
sideSwipeView = nil;
slide = NO;
} else {
//options do not exist and therefore we need to add it
NSArray * buttonData = [[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Mark Read", @"title", @"mark.png", @"image", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Track", @"title", @"play.png", @"image", nil],
nil] retain];
NSMutableArray * buttons = [[NSMutableArray alloc] initWithCapacity:buttonData.count];
sideSwipeView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-25, 320, 25)];
[sideSwipeView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[sideSwipeView setBackgroundColor:[UIColor colorWithPatternImage: [UIImage imageNamed:@"dotted-pattern.png"]]];
[sideSwipeView setTag:-10];
CGFloat leftEdge = BUTTON_LEFT_MARGIN;
for (NSDictionary* buttonInfo in buttonData)
{
if (!([[buttonInfo objectForKey:@"title"] isEqualToString:@"Mark Read"] && [[[topics objectAtIndex:indexPath.row] unread] intValue] == 0))
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
UIImage* buttonImage = [UIImage imageNamed:[buttonInfo objectForKey:@"image"]];
if ([[topics objectAtIndex:indexPath.row] tracked] && [[buttonInfo objectForKey:@"title"] isEqualToString:@"Track"]){
buttonImage = [UIImage imageNamed:@"pause.png"];
[button setSelected:YES];
} else {
[button setSelected:NO];
}
button.frame = CGRectMake(leftEdge, 0, buttonImage.size.width, buttonImage.size.height);
UIImage* grayImage = [self imageFilledWith:[UIColor colorWithWhite:0.9 alpha:1.0] using:buttonImage];
[button setImage:grayImage forState:UIControlStateNormal];
if ([[buttonInfo objectForKey:@"title"] isEqualToString:@"Mark Read"]){
[button addTarget:self action:@selector(markRead:) forControlEvents:UIControlEventTouchUpInside];
} else if ([[buttonInfo objectForKey:@"title"] isEqualToString:@"Track"]){
[button addTarget:self action:@selector(track:) forControlEvents:UIControlEventTouchUpInside];
}
[button setTag:indexPath.row];
[buttons addObject:button];
[sideSwipeView addSubview:button];
leftEdge = leftEdge + buttonImage.size.width + BUTTON_SPACING;
}
}
[cell.contentView addSubview:sideSwipeView];
[sideSwipeView release];
global = indexPath;
slide = YES;
}
[self.tableView endUpdates];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
但是,我沒有信心,代碼是正確的內存(即:療法沒有內存泄漏,我不濫用任何東西)。我大多對sideSwipeView alloc,dealloc沒有把握,並將其設置爲零。我認爲其他人都很好。有人能給我任何指針嗎?
探查結果:
在仔細研究您的代碼之前,您是否運行過分析儀?在Xcode 4中,它位於產品菜單「分析」下。在Xcode 3中,我相信它在Build菜單中是「構建和分析」。如果這是明確的,你仍然擔心,然後問這裏是有道理的,但如果你沒有運行它,我想先建議它,這樣你就可以解決它找到的任何東西。 – 2011-05-17 00:59:27
我沒有運行一個探查器,圖像顯示在上面..不知道它是否與sideSwipeView有關或不像..看起來像它與按鈕有關 – aherlambang 2011-05-17 01:06:23
它看起來像你需要釋放線229/234。很難準確地說出他們沒有行號的情況。 – csano 2011-05-17 01:14:27