UICollectionView adding image and button - xcode

I used UICollectionView for displaying the images from CameraRoll, it is working fine, now I want to add the Camera button in the collectionview cell. I had written the code like this, button is not getting displayed.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"collectionCell" forIndexPath:indexPath];
[self.cameraRoll thumbAtIndex:indexPath.row completionHandler:^(UIImage *thumb) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:thumb];
imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
//Now Create Button
UIImage* img = [UIImage imageNamed:#"btn_take_pic"];
UIImageView *btnImage = [[UIImageView alloc] initWithImage:img];
btnImage.frame = CGRectMake(0,0, 50,50);
btnImage.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
btnImage.contentMode = UIViewContentModeScaleAspectFill;
//End of create button
[cell.contentView addSubview:btnImage];//Add Button to the cell
}];
return cell;
}

Create Button like this
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"collectionCell" forIndexPath:indexPath];
//Add your image showing code here
//Now Create Button in cell
CGRect btnRect = CGRectMake(0, 0 , 30 , 30);
UIButton *cellBtn = [[UIButton alloc] initWithFrame:btnRect];
[cellBtn setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[cellBtn setTitle:#"Text to Show" forState:UIControlStateNormal];
[cell.contentView addSubview:cellBtn];
[[cell cellBtn] addTarget:self action:#selector(CellBtnTapped:event:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
method for button tap
-(void)CellBtnTapped:(id)sender event:(id)event
{
// perform your action here that you want to do on button tap
}
But It is good that you should make a separate class for your CollectionViewCell and add all objects there, that you want to take in your collectionView cell.

This is the solution for the issue I mentioned above, I think it might be helpful for others, so positing the code here
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"collectionCell" forIndexPath:indexPath];
if(indexPath.row>0)
[self.cameraRoll thumbAtIndex:(indexPath.row-1) completionHandler:^(UIImage *thumb) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:thumb];
imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
}];
else{
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_take_pic.png"]];
imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
}
return cell;
}

Related

Copy UIImage to clipboard on tap

I've got a UICollectionView which displays as many cells as the amount of images passed through.
I want to make it so that when you tap an image, it copies that image to the clipboard.
Any help would be greatly appreciated.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *Cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"ImagesCell"
forIndexPath:indexPath];
UIImage *image;
int row = indexPath.row;
//set image of this cell
image = [UIImage imageNamed:localImages.imageFiles[row]];
Cell.imageCell.image = image;
//enable touch
[Cell.imageCell setUserInteractionEnabled:YES];
Cell.imageCell.tag = row;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureRecognizer:)];
[tapGesture setNumberOfTapsRequired:1];
[Cell.imageCell addGestureRecognizer:tapGesture];
return Cell;
}
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender
{
NSInteger string = (sender.view.tag);
NSLog(#"this is image %i",string);
UIImage *copiedImage = I don't know what to put here..
[[UIPasteboard generalPasteboard] setImage:copiedImage];
}
It's actually easier if you just implement the delegate method of UICollectionView - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath and just do this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
UIImage *copiedImage = cell.imageCell.image;
[[UIPasteboard generalPasteboard] setImage:copiedImage];
}
Hope this helped you out!
Edit: In case you have different type of cells with images and text and you only want to enable copying under images, you will have to implement that UITapGestureRecognizer part and in the method called you just need to do this:
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender
{
UIImageView *imageView = (UIImageView *)sender.view;
UIImage *copiedImage = imageView.image;
[[UIPasteboard generalPasteboard] setImage:copiedImage];
}
Since your UITapGestureRecognizer is attached to the UIImageView, it is the sending view and you can extract the image directly from it and copy to the pasteboard.

How open another viewController when i pushed on any row in UITableView?

Help me please with my tableView in xCode.
So i have my code:
-(void)viewDidLoad {
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, -150, 45)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 400, 40)] autorelease];
headerLabel.text = #"My table";
headerLabel.textColor = [UIColor greenColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tableView.tableHeaderView = containerView;
self.tableData = [NSArray arrayWithObjects:#"Eggs",#"Milk", #"Chocolate", #"Drink", #"Banana", #"Apple", #"Fruit", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//Поиск ячейки
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIViewController *Main = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
[self.navigationController pushViewController:Main animated:YES];
UIViewController *Main2 = [self.storyboard instantiateViewControllerWithIdentifier:#"Main2"];
[self.navigationController pushViewController:Main2 animated:YES];
}
When i pushed on the row "Eggs" will open another viewController with eggs. But i want that, when i will push on the row "Milk" will open page with milk but when i pushed on the row #"milk" i have page with "Eggs"...Thanks for help...big thanks. I think it work with index..but i don't know.
Try it....
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
UIViewController *Main = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
[self.navigationController pushViewController:Main animated:YES];
}
if (indexPath.row == 1)
{
UIViewController *Main2 = [self.storyboard instantiateViewControllerWithIdentifier:#"Main2"];
[self.navigationController pushViewController:Main2 animated:YES];
}
}
Hope it will help you....

longpress custom items UICollectionViewCell image change

How can I make a cell imageview to change after longpress gesture?
With this one when I click on a cell (longpress) the 4 customized items appear but when I select one of them the app crashes. (if you remove :(Cell*)cell and cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"ICUbedRED.png"]]; it works...I mean the alertView appears but of course the image doesn't change).
- (void)longPress:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
Cell *cell = (Cell *)recognizer.view;
[cell becomeFirstResponder];
UIMenuItem *highDep = [[UIMenuItem alloc] initWithTitle:#"High Dependency" action:#selector(hiDep:)];
UIMenuItem *lowDep = [[UIMenuItem alloc] initWithTitle:#"Low Dependency" action:#selector(lowDep:)];
UIMenuItem *booked = [[UIMenuItem alloc] initWithTitle:#"Booked" action:#selector(booked:)];
UIMenuItem *free = [[UIMenuItem alloc] initWithTitle:#"Free" action:#selector(free:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:booked, highDep, lowDep, free, nil]];
[menu setTargetRect:cell.frame inView:cell.superview];
[menu setMenuVisible:YES animated:YES];
}
}
the voids are:
- (void)hiDep:(Cell*)cell
{
NSLog(#"Bed is HiDep");
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"ICUbedRED.png"]];
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:#"This Bed is High Dependency"
message:#""
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[testAlert show];
[testAlert release];
}
- (void)lowDep:(Cell*)cell
{.
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"ICUbedYELLOW.png"]];
..}
- (void)free:(Cell*)cell
{..
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"ICUbedGREEN.png"]];
.}
- (void)booked:(Cell*)cell
{..
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"ICUbedBLUE.png"]];
.}
and the cell building method is:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
Cell *cvc = (Cell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
int i = indexPath.row%[labelArray count];
number = i;
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[cvc addGestureRecognizer:recognizer];
cvc.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"icubed.png"]];
cvc.label.text = [labelArray objectAtIndex:number];
return cvc;
}
#dottorfeelgood It is crashing for
cell.imageView.image = [UIImage imageNamed:[NSString stringWi......
because, the object retured as param to methods like
(void)lowDep:(Cell*)cell
is not of Class Cell, the retured param is of class UIMenuItem. because you are clicking on menuItems not Cell.
Instead of doing what you are doing now, you can use the MenuItems and corresponding actions on UICollectionCell solution provided by UICollectionView by default. You can check this tutorial here!
Just implement the 3 delegate methods and
// These methods provide support for copy/paste actions on cells.
// All three should be implemented if any are.
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;
and set your custom menuItems needed to the sharedMenuController in ViewdidLoad.
Hope this helps, excuse my bad sentence forming.

How to get indexPath in a tableview from a button action in a cell?

I have a tableview with two actions in it, for the first on i use the delegate didSelectRowAtIndexPath, for the second one i would like to use an action on a button on my cell to do something else. My problem is that i don't succeed to get the indexpath ? What is the best practice to do that.
If you have added the button to the cell as,
[cell.contentView addSubview:button];
then, you can get the index path as,
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Go ahead
}
I'm using this solution -- getting index path of cell using points
CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(#"%#",indexPath);
Its work perfectly
here is the full code for a custom button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
//configure your cell here
cell.titleLabel.text = #"some title";
//add button
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(40, 10, 18, 18);
[resetButton addTarget:self action:#selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[resetButton setImage:[UIImage imageNamed:#"someimage.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:myButton];
//afterwards return the cell
return cell;
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [filterTable indexPathForCell:cell];
NSLog(#"%#", indexPath);
//use indexPath here...
}
In case you are using a collection view, you can use Yogesh method, but change indexPathForRowAtPoint for indexPathForItemAtPoint like this:
CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(#"%#",indexPath);
In case that your button is moved or Apple changes the view hierarchy for accessory views, this method goes up the chain to look for a UITableViewCell:
- (void)tapAccessoryButton:(UIButton *)sender {
UIView *parentView = sender.superview;
// the loop should take care of any changes in the view heirarchy, whether from
// changes we make or apple makes.
while (![parentView.class isSubclassOfClass:UITableViewCell.class])
parentView = parentView.superview;
if ([parentView.class isSubclassOfClass:UITableViewCell.class]) {
UITableViewCell *cell = (UITableViewCell *) parentView;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}

xcode uitableview cell image align

I'm sure this is very simple but i can't find any documentation about it..
in my uitableview i have the text labels aligned to the right (my app is in heberw which is rtl)
i'm trying to add an image to the cell to the RIGHT of the text label, no luck, so far the image is aligned to the left and i can't find a way to align it to the right side of the cell.
here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:#"tableicon.png"];
NSString *cellValue = [[stories objectAtIndex:indexPath.row] objectForKey:#"ArticleTitle"];
cell.textLabel.text = cellValue;
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.textLabel.font = [UIFont systemFontOfSize:17];
cell.textLabel.numberOfLines = 2;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
Instead of adding the image as a subview of your UITableViewCell, add a container with right aligned subviews.
UIImageView *pic = myImageView;
UIView *picContainer = [[UIView alloc] initWithFrame:cell.frame];
picContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
picContainer.contentMode = UIViewContentModeRight;
pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
pic.center = CGPointMake(picContainer.frame.size.width-70, picContainer.frame.size.height/2);
[picContainer addSubview:pic];
[cell.contentView addSubview:picContainer];
This has the added benefit of automatically readjusting when the orientation changes.
try this one
cell.accessoryView=[UIImage imageNamed:#"tableicon.png"];

Resources