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

标签 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。

请帮帮我!

最佳答案

您可以访问通过实现 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 选择的 UITableViewCellNSIndexPath ; 方法。

当您触摸该单元格以添加照片时,它会触发该方法。然后,您可以通过 indexPath.row 参数访问该行。

编辑

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

改变

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

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];

关于ios - 在打开照片库并选择一张照片以在单击的单元格中设置此照片后,获取我单击的单元格的 IndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20201854/

相关文章:

ios - 如何连接到 UITableViewController

ios - 如何在排序数组中将一行代码从 Objective-C 转换为 Swift?

iphone - 您可以从 UITableViewDataSource 函数中访问应用程序委托(delegate)吗?

ios - UITableView 单元格背景颜色没有改变?

ios - 获取适当的 UITableViewCell 分隔 : GUI

objective-c - 如何阻止 HIToolbox 捕获我的异常?

ios - Facebook ID 字符串长度

ios - 插页式实现

objective-c - 为自定义单元格设置 uitableviewcell 高度

iphone - 在 UINavigationController 中推送同一类 View Controller 的不同对象