如何渲染UITableViewCell

时间:2011-11-21 17:28:24

标签: iphone objective-c cocoa-touch ipad uitableview

我创建了UITabBar模板。然后我向firstViewController添加一个tableView。建立连接(UITableViewDataSource和UITableViewDelegate)。 在didselect方法中,我需要渲染选定的UITableViewCell并将其设置为UITabBars之一。 如果我渲染UITableViewCell row = 0 - 没关系,但如果我渲染UITableViewCell行> 0(1,2,...)我得到黑色矩形。 我可以渲染UITableViewCell contentView,但它将在没有背景的情况下渲染。 如何以背景获取UITableViewCell的快照?

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}


#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController <UITableViewDelegate,     UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *tableViewAnimation;
@property (nonatomic, retain) UIImageView *thumbnail;

- (UIImage*)screenshotFromView:(UIView *)view;

@end


#import "FirstViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation FirstViewController
@synthesize tableViewAnimation = _tableViewAnimation;
@synthesize thumbnail = _thumbnail;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}


#pragma mark - UITableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
    NSUInteger count = 10;
    return count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *goodsListCellIdentifier = @"ListOfGoodsIndetifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:goodsListCellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsListCellIdentifier];
        cell.textLabel.backgroundColor = [UIColor clearColor];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = @"detalied view";

return cell;
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [_tableViewAnimation cellForRowAtIndexPath:indexPath];
    CGRect imageViewFrame = cell.frame;
    imageViewFrame.origin.y = 64.0 + tableView.frame.origin.y + imageViewFrame.origin.y     - tableView.contentOffset.y;
//    imageViewFrame.size.width = cell.contentView.frame.size.width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];

    imageView.image = [self screenshotFromView:cell];
    [self.tabBarController.view addSubview:imageView];
    self.thumbnail = imageView;
    self.thumbnail.hidden = NO;
    self.view.userInteractionEnabled = NO;
    [UIView animateWithDuration:1.0 
                     animations:^{
                         CGRect tabBarFrame =  self.tabBarController.tabBar.frame;
                         CGFloat xoffset = tabBarFrame.size.width / 2;
                         [_thumbnail setCenter:CGPointMake(xoffset * 0.5, tabBarFrame.origin.y + 25.0)];
                         [_thumbnail setTransform:CGAffineTransformMakeScale(0.1, 0.1)];
                         [_thumbnail setAlpha:0.4];
                     }
                     completion:^(BOOL finished){
                         [self.thumbnail removeFromSuperview];
                         self.thumbnail = nil;
                         self.view.userInteractionEnabled = YES;
                     }];
}

- (UIImage*)screenshotFromView:(UIView *)view {
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // -renderInContext: renders in the coordinate space of the layer,
    // so we must first apply the layer's geometry to the graphics context
    CGContextSaveGState(context);
    // Center the context around the window's anchor point
    CGContextTranslateCTM(context, [view center].x, [view center].y);
    // Apply the window's transform about the anchor point
    CGContextConcatCTM(context, [view transform]);
    // Offset by the portion of the bounds left of and above the anchor point
    CGContextTranslateCTM(context, -[view bounds].size.width * [[view layer] anchorPoint].x, -[view bounds].size.height * [[view layer] anchorPoint].y);

    // Render the layer hierarchy to the current context
    [[view layer] renderInContext:context];

    // Restore the context
    CGContextRestoreGState(context);

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   return image;
}

@end

2 个答案:

答案 0 :(得分:0)

背景可能不是由单元格自动渲染,而是由表格自动渲染。 请注意,渲染背景取决于单元格状态(背景/所选背景)。

您可以在渲染单元格之前获取cell.backgroundView并渲染它。 [((UITableViewCell*) view).backgroundView.layer renderInContext:context]

但这只是猜测。

也许它更简单。点击单元格时,将启动选择动画。 renderInContext忽略了动画。也许有问题?

答案 1 :(得分:0)

只需评论下一行:

// Center the context around the window's anchor point
//    CGContextTranslateCTM(context, [view center].x, [view center].y);
// Apply the window's transform about the anchor point
//    CGContextConcatCTM(context, [view transform]);
// Offset by the portion of the bounds left of and above the anchor point
//    CGContextTranslateCTM(context, -[view bounds].size.width * [[view layer] anchorPoint].x, -[view bounds].size.height * [[view layer] anchorPoint].y);