I've a tableView with dynamic cells. Each section has 5 rows. In the fifth row there is a button and an imageview. When (at runtime) I click on the button, I can choose a photo from my photo library. The image should be put in imageview. This doesn't happen because I can't get the indexPath of the cell which I clicked the button to add a photo. The number of sections is decided at run-time using a stepper.
Here the code that will be executed when I click on the button (to open the photo library)
//method connected to the selected button photo
- (IBAction)selectPhoto:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take a photo",@"Pick from library", nil];
[self getIndexPathAtSection:sender];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[self shootPictureOrVideo];
}
else if (buttonIndex == 1){
[self selectExistingPictureOrVideo];
}
}
#pragma mark - CAMERA AND PHOTO LIBRARY
- (void)pickMediaFromSource:(UIImagePickerControllerSourceType)sourceType
{
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerController isSourceTypeAvailable:sourceType] && [mediaTypes count]>0){
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.mediaTypes = mediaTypes;
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:NULL];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing media" message:@"Device doesn't support that media source" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil, nil];
[alert show];
}
}
- (UIImage *)shrinkImage:(UIImage *)original toSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[original drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *final = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return final;
}
- (void)shootPictureOrVideo
{
[self pickMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
- (void)selectExistingPictureOrVideo
{
[self pickMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
#pragma mark - IMPAGE PICKER CONTROLLER DELEGATE
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *CellIdentifier = @"cellProfileSnap";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:_indexForPicker];
UIImageView *imgViewPhoto = (UIImageView *) [cell viewWithTag:4];
_lastChosenMediaType = info[UIImagePickerControllerMediaType];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
UIImage *shrunkenImage = [self shrinkImage:chosenImage toSize:imgViewPhoto.bounds.size];
imgViewPhoto.image = shrunkenImage;
NSLog(@"%ld - %ld %ld",(long)_indexForPicker.row,(long)imgViewPhoto.tag,(long)cell.tag);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)getIndexPathAtSection:(id)sender{
UITableViewCell *uhm = [self.tableView dequeueReusableCellWithIdentifier:@"cellProfileSnap"];
NSIndexPath *indexPath = [self.tableView indexPathForCell:uhm];
_indexForPicker = indexPath;
NSLog(@"%ld",(long)_indexForPicker.row);
}
The _indexPathForPicker is always 0.
Please help me!
You can access the NSIndexPath
of the UITableViewCell
selected by implementing the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
method.
When you touch that cell to add a photo, it will fire that method. You can then access the row via the indexPath.row
argument.
edit
You should also not dequeue a cell yourself. You should acquire a cell by using tableView:cellForRowAtIndexPath:
Change
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:_indexForPicker];
to
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];