gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:28:02 25 4
gpt4 key购买 nike

我有一个带有动态单元格的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com