Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to render UITableViewCell
    primarykey
    data
    text
    <p>I create UITabBar template. Then I add to firstViewController a tableView. Make connections (UITableViewDataSource and UITableViewDelegate). In didselect method I need to render selected UITableViewCell and animate it to one of UITabBars. If I render UITableViewCell row = 0 - it's ok, But if I render UITableViewCell row > 0 (1,2,...) I get black rectangle. I can render UITableViewCell contentView, but it will render without background. How can I get snapshot of UITableViewCell with a background?</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface AppDelegate : UIResponder &lt;UIApplicationDelegate, UITabBarControllerDelegate&gt; @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 &lt;UIKit/UIKit.h&gt; @interface FirstViewController : UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt; @property (nonatomic, strong) IBOutlet UITableView *tableViewAnimation; @property (nonatomic, retain) UIImageView *thumbnail; - (UIImage*)screenshotFromView:(UIView *)view; @end #import "FirstViewController.h" #import &lt;QuartzCore/QuartzCore.h&gt; @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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload