获取打开照片库后单击的单元格的IndexPath,并选择照片以在单击的单元格中设置此照片

时间:2013-11-25 19:44:47

标签: ios objective-c uitableview nsindexpath

我有一个带有动态单元格的tableView。每个部分有5行。在第五行中有一个按钮和一个imageview。当(在运行时)我点击按钮时,我可以从我的照片库中选择一张照片。图像应放在imageview中。这不会发生,因为我无法获取单击按钮添加照片的单元格的indexPath。使用步进器在运行时决定节数。

enter image description here

这里是我点击按钮(打开照片库)时要执行的代码

//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);
}

_indexPathForPicker始终为0.

请帮助我!

1 个答案:

答案 0 :(得分:1)

您可以通过实施NSIndexPath方法访问所选UITableViewCell的{​​{1}}。

当您触摸该单元格以添加照片时,它将触发该方法。然后,您可以通过- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;参数访问该行。

修改

你也不应该自己出列一个单元格。您应该使用tableView:cellForRowAtIndexPath:

获取一个单元格

更改

indexPath.row

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:_indexForPicker];