Note that there are some explanatory texts on larger screens.

plurals
  1. POPlacing UIImage at tapped image in UITableViewCell
    primarykey
    data
    text
    <p>Currently I have custom UITablViewCell's (BIDSelectBusinessCustomCell) that are displayed in a tableView (myTableView). The custom cell is made up of a UIImageView and a label. When the view loads the labels are populated with the strings from my model. UIImageViews are blank. I want to be able for a user to 'tap' the UIImageView, select a picture from what is stored on their phone and for that image to be saved to the UIImageView.</p> <p>From the below code I can get the 'tap' gesture, then the pickercontroller pops up and a user selects an image. The way the code is now one image that is selected is set for all of the UIImageViews. Which is understandable. But I want it to be set to that particular UIImageView and don't know how to.</p> <p>Code: </p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface BIDBusinessSelectViewController : UIViewController &lt;UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate&gt; @property (weak, nonatomic) IBOutlet UITableView *myTableView; @property (strong, nonatomic) NSArray *linkedBusinessParseArray; //Stores the array of models @property (strong, nonatomic) NSMutableArray *linkedBusinessParseModelArray; @property NSUInteger relevantIndex; @property (strong, nonatomic) UIImage *tempImageHolder; @end #import "BIDBusinessSelectViewController.h" #import &lt;Parse/Parse.h&gt; #import "BIDBusinessModel.h" #import "BIDSelectBusinessCustomCell.h" @interface BIDBusinessSelectViewController () &lt;ImageSelect&gt; { BIDSelectBusinessCustomCell *aCell;//define a cell of ur custom cell to hold selected cell UIImage *choosenImage; //image to set the selected image } @end @implementation BIDBusinessSelectViewController - (void)viewDidLoad { [super viewDidLoad]; self.myTableView.delegate = self; self.myTableView.dataSource = self; self.linkedBusinessParseModelArray = [[NSMutableArray alloc]init]; //create query PFQuery *linkedBusinessParseQuery = [PFQuery queryWithClassName:@"linkedBusinessParseClass"]; //follow relationship [linkedBusinessParseQuery whereKey:@"currentBusinessUserParse" equalTo:[PFUser currentUser]]; [linkedBusinessParseQuery whereKey:@"linkRequestSentParse" equalTo:@"Approved"]; [linkedBusinessParseQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { self.linkedBusinessParseArray = objects; //store them in my model array //loop through model array from Parse for (NSDictionary *dict in self.linkedBusinessParseArray) { NSString *descPlaceNameParse = [dict objectForKey:@"placeDescriptionParse"]; NSLog(@"descPlacesNameParse: %@",descPlaceNameParse); PFObject *tempObj = (PFObject *) dict; NSString *tempObjString = tempObj.objectId; NSLog(@"tempObjString (inside dict): %@", tempObjString); //storing values from Parse into my model BIDBusinessModel *userModel = [[BIDBusinessModel alloc]init]; userModel.descriptionModelParse = descPlaceNameParse; userModel.objectIdModelParse = tempObjString; [self.linkedBusinessParseModelArray addObject:userModel]; NSLog(@"self.linkedBusinessParseModelArray: %lu", (unsigned long)[self.linkedBusinessParseModelArray count]); //Reload tableview. Has to go here in block otherwise it does not occur [self.myTableView reloadData]; } }]; if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Device has no camera" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [myAlertView show]; } choosenImage = [UIImage imageNamed:@"pin.png"]; //hear u need to set the image for cell assuming that u are setting initially same image for all the cell } #pragma mark - #pragma mark Table Delegate Methods -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.linkedBusinessParseModelArray.count; //returns count of model NSMutableArray } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"Start cellforRowStIndex"); static NSString *CellIdentifier = @"Cell"; BIDSelectBusinessCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[BIDSelectBusinessCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } BIDBusinessModel *bizModel; bizModel = self.linkedBusinessParseModelArray[indexPath.row]; bizModel.image = choosenImage; //cell.descLabel.text = [NSString stringWithFormat:@"bid= %d",indexPath.row];//set text from the model//Omitted for my desc cell.descLabel.text = bizModel.descriptionModelParse; cell.logoImage.image =bizModel.image; //setting the image initially the image when u set in "viewDidLoad" method from second time onwords it will set from the picker delegate method //insted of settig the gesture hear set it on the custom cell cell.ImageSelectDelegate = self; //setting the delegate cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } // hear implementation of delegate method - (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView; { //open up the image picker UIImagePickerController *picker = [[UIImagePickerController alloc]init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; aCell = (BIDSelectBusinessCustomCell *)logoImgView.superview; //her getting the cell [self presentViewController:picker animated:YES completion:NULL]; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *aChosenImage = info[UIImagePickerControllerEditedImage]; //UIImage holder to transfer to cellForRowAtIndexPath choosenImage = aChosenImage; NSIndexPath *indexPath = [self.myTableView indexPathForCell:aCell]; [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //hear reloading the selected cell only not entire tableview //get the model and set the choosen image BIDBusinessModel *bizModel; bizModel = self.linkedBusinessParseModelArray[indexPath.row]; bizModel.image = aChosenImage; [picker dismissViewControllerAnimated:YES completion:NULL]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:NULL]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end #import &lt;UIKit/UIKit.h&gt; @protocol ImageSelect&lt;NSObject&gt; //for this u need use custom delegate so i did like this - (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView; @end @interface BIDSelectBusinessCustomCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *descLabel; @property (strong, nonatomic) IBOutlet UIImageView *logoImage; @property (nonatomic, assign) id&lt;ImageSelect&gt;ImageSelectDelegate; // deleagte @end #import "BIDSelectBusinessCustomCell.h" @implementation BIDSelectBusinessCustomCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.logoImage = [[UIImageView alloc]init]; self.descLabel = [[UILabel alloc]init]; //set up gesture hear in the custom cell insted in the controller class UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLogoImage:)]; [tap setNumberOfTouchesRequired:1]; [tap setNumberOfTapsRequired:1]; [tap setDelegate:self]; self.logoImage.userInteractionEnabled = YES; [self.logoImage addGestureRecognizer:tap]; //[self addSubview:logoImage]; //[self addSubview:descLabel]; } return self;} - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)tapLogoImage:(UITapGestureRecognizer *)guesture { if([self.ImageSelectDelegate respondsToSelector:@selector(selectSetImageForSelectedLogImage:)]) { [self.ImageSelectDelegate selectSetImageForSelectedLogImage:self.logoImage];//call the delegate method from the selected cell } } @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