0

I'm trying to drag & drop a customCell from a tableView. I'm using an UIImageView that gets the image of the customCell and then drag it around the view but the problem is that I can't get the image of the customCell.

CustomCellClass *customCell = [[CustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CustomCellClass identifier]];

customCell.textLabel.text = "Dragging cell";
_draggedImageView.image = customCell.imageRepresentation;

But this doesn't work, it doesn't get any image.

I also tried:

 CustomCellClass *customCell = [tableView dequeueReusableCellWithIdentifier:[CustomCellClass identifier] forIndexPath:0];

customCell.textLabel.text = "Dragging cell";
_draggedImageView.image = customCell.imageRepresentation;

This works but there's a problem. Since I'm changing the text of the reused customCell it'll change in the tableView too. I need a copy of the customCell so I can have the correct layout, change what I want in that copy (in this case the textLabel of the cell) and add it the imageView to drag, and this separated from any customCell of the tableView.

Any ideas how to achieve this?

Thank you all.

lopes710
  • 315
  • 1
  • 3
  • 19

2 Answers2

0

I don't know about the existance of -imageRepresentation in UIKit. Usually to create an image from the backing store, you should create a bitmap context, render the view inside that context and finally get the image representation. You can create a category such as this:

#import "UIView+RenderVIew.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIView (RenderView)

- (UIImage *) imageByRenderingViewOpaque:(BOOL) yesOrNO {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, yesOrNO, 0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}
- (UIImage *) imageByRenderingView{
    return [self imageByRenderingViewOpaque:NO];
}

@end
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • That didn't work. By the way, the customCell in the storyboard has the style:"Custom". I tried using initWithStyle:@"Custom" in the customCell initialization but doesn't work also. Any ideas? – lopes710 Jun 07 '13 at 09:17
  • You aren't using it correctly... I'm using that few lines of code since 2011, always worked – Andrea Jun 07 '13 at 09:30
  • Ok guys, so the solution was simply to use this method without the indexPath: CustomCellClass *customCell = [tableView dequeueReusableCellWithIdentifier:[CustomCellClass identifier]]; Thanks for your time! – lopes710 Jun 07 '13 at 10:05
0

I did it with create a UIView Category, Add a method to it, and when you want image of any type of view(CustomCell) call this function It will return an image of that particular view.

@implementation UIView (UIViewCategory)

- (UIImage*)convertInImage {

    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end

How to use:

UIImage *cellImage = [yourCustomCellObj convertInImage];

It is working in my case, hope it will solve your problem.

Prateek Prem
  • 1,544
  • 11
  • 14