简体   繁体   English

目标C-使用tableview将选择的图像从UIImageView传递/检索到第二个VC,以便单元格可以显示该图像

[英]Objective C - Passing/retrieving selected image from UIImageView to second VC with tableview so cell can display that image

i already passed input text with prepareForSegue, text into mutablearray, then passing that array with prepareforeSegue to second VC with tableview so table cell now displays name. 我已经使用prepareForSegue传递了输入文本,将文本传递到mutablearray中,然后使用prepareforeSegue将该数组传递给具有tableview的第二个VC,因此表单元格现在显示名称。 But what with selected/picked image in uIImageView in first vc so table cell in second VC can display image in a row next to name? 但是在第一个vc中的uIImageView中选择/挑选的图像会怎样,以便第二个VC中的表格单元格可以在名称旁边的行中显示图像? i manage to do it with image from imagexxassets i followed the same logic like passing text but keep failing. 我设法用来自imagexxassets的图像来做到这一点,我遵循与传递文本相同的逻辑,但是一直失败。

VIewController.h VIewController.h

    #import <UIKit/UIKit.h>
    #import "TableViewController.h"
    #import <MobileCoreServices/MobileCoreServices.h>

    @interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
    @property (strong, nonatomic) IBOutlet UITextField *inputName;

    @property (nonatomic) NSMutableArray *items;
    //@property (nonatomic) NSMutableArray *images;

    - (IBAction)AddData:(id)sender;

    @property (strong, nonatomic) IBOutlet UIImageView *imageView;

    @property (strong, nonatomic) IBOutlet UIButton *selectPhoto;
    @property (strong, nonatomic) IBOutlet UIButton *takePhotoFromCamera;
    @end

ViewController.m ViewController.m

    #import "ViewController.h"

    @interface ViewController ()


    @end

    @implementation ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        _items = [[NSMutableArray alloc]init];
        //_images = [[NSMutableArray alloc]init];
        [self.takePhotoFromCamera addTarget:self action:@selector(_takePhoto) forControlEvents:UIControlEventTouchUpInside];
        [self.selectPhoto addTarget:self action:@selector(_selectPhoto) forControlEvents:UIControlEventTouchUpInside];}

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (IBAction)AddData:(id)sender
    {
        //[_images addObject:self.imageView.image];
        //[_images insertObject:self.imageView.image atIndex:0];
        if ([self.inputName.text length]> 0)
        {
            [_items addObject:self.inputName.text];
            //[_images addObject:self.imageView.image];

            [self performSegueWithIdentifier:@"tableSegue" sender:self];
        }
        else
        {
            UIAlertView *alertView =  [[UIAlertView alloc]initWithTitle:@"Error"
                                                                message:@"You must enter some data"
                                                               delegate:self
                                                      cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertView show];
        }

    }

    - (void)addItemViewController:(TableViewController *)controller didFinishSelectingItem:(NSMutableArray *)item selectedTag:(int)tag{
        NSLog(@"DATA=%@", item);

    }

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([segue.identifier isEqualToString:@"tableSegue"])
        {

            TableViewController *controller = (TableViewController *)segue.destinationViewController;
            controller.items = _items;
            //TableViewController *controller1 = (TableViewController *)segue.destinationViewController;
            //controller1.images = _images;
            NSLog(@"%@",self.inputName.text);
            self.inputName.text = NULL;
        }
    }


    #pragma mark code for images
    -(void)_takePhoto
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:picker animated:YES completion:NULL];
    }

    -(void)_selectPhoto
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];

    }

    -(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        [self dismissViewControllerAnimated:NO completion:nil];
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
        {
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

            self.imageView.image = image;
        }
    }

    -(void)imagePickerControllerDidCancel:
    (UIImagePickerController *)picker
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end

TableViewController.h TableViewController.h

    #import <UIKit/UIKit.h>


    //using delegate
    @protocol TableViewControllerDelegate <NSObject>
    - (void)addItemViewController:(id)controller didFinishSelectingItem:(NSMutableArray *)item selectedTag:(int)tag;

    @end
                                    //extra attention here
    @interface TableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

    // id of delegate
    @property (nonatomic, weak) id <TableViewControllerDelegate> delegate;

    @property (nonatomic) NSMutableArray *items;
    //@property (nonatomic) NSMutableArray *images;

    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    @end

TableViewController.m TableViewController.m

    #import "TableViewController.h"

    @interface TableViewController ()


    @end

    @implementation TableViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // code for reversing objects in array so tableview can put new rows on top
        //_items = [[[_items reverseObjectEnumerator] allObjects] mutableCopy];

        self.tableView.delegate = self;
        self.tableView.dataSource = self;

        // setting invisible footer to display only rows which contains data and to remove border lines in tableview
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
        //NSString *lijevaSlika = [NSString stringWithFormat:@"dice%i", sum];
        //[questionTexts objectAtIndex:0]
       //NSString *selectedImage = [NSString stringWithFormat:_images];

        myCell.imageView.image = [UIImage imageNamed:@"2.png"];
        myCell.textLabel.text = _items[indexPath.row]; //name of objects in array are displayed in cells
        myCell.textLabel.textColor = [UIColor blueColor]; //changing color of text in cell

        //changing background of cell
        /*myCell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
         myCell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"front.png"]];
         */


        return myCell;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {

        return [self.items count]; //objects in arrays - rows in table

    }

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {

        [_items removeObjectAtIndex:indexPath.row]; //swipe to left do delete row
        [self.tableView reloadData];
    }

    /*- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //selected row returns to previous viewcontroller
        [self.navigationController popViewControllerAnimated:YES];
    }
    */



    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    @end

首先您必须将选定的图像存储在数组中,然后必须以与传递_items数组相同的方式传递该数组。然后在TableViewController cellForRowAtIndexPath中添加此行并检查是否在_images数组中获取了任何内容

myCell.imageView.image =  [_images objectAtIndex:indexPath.row];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 目标C-将数据从具有表视图的第二个VC传递到第一个VC - Objective C - Passing data from second VC with tableview to first VC 如何在目标C中将图像从主viewController设置为tableView单元格? - How to set image from main viewController to tableView cell in objective C? 从 photoLibrary 获取图像并显示在 tableview 单元格上? - get image from photoLibrary and display on tableview Cell? iOS Swift UIImageView在tableView单元格中更改图像 - iOS swift UIImageView change image in tableView cell 如何调整图像大小以适应表视图单元格中的 UIImageview - how to resize the image to fit in UIImageview in a tableview cell 无法从目标C中的SQL数据库检索图像 - Image not retrieving from sql database in objective c 将具有某些属性的核心数据从TableView中的选定单元格传递到另一个VC - Passing core data from selected cell in tableview with certain attributes to another VC 在单元格图像中传递选定的图像 - Passing selected Image in cell image 想要从仅具有图像名称的api将图像显示到tableview单元格中 - want to display image into tableview cell from an api with only image name 如何将选定的tableview单元格文本发送到iOS中的前一个视图控制器,Objective C(向后传递数据) - How to send selected tableview cell text to previous view controller in iOS, Objective C (passing data backward)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM