1

現在、tableView (myTableView) に表示されるカスタム UITablViewCell (BIDSelectBusinessCustomCell) があります。カスタム セルは、UIImageView とラベルで構成されます。ビューが読み込まれると、モデルの文字列がラベルに取り込まれます。UIImageViews は空白です。ユーザーがUIImageViewを「タップ」し、携帯電話に保存されているものから画像を選択し、その画像をUIImageViewに保存できるようにしたいと考えています。

以下のコードから「タップ」ジェスチャを取得できます。次に、ピッカーコントローラーがポップアップし、ユーザーが画像を選択します。コードが選択された 1 つの画像になる方法は、すべての UIImageView に対して設定されます。これは理解できます。しかし、その特定の UIImageView に設定したいのですが、その方法がわかりません。

コード:

#import <UIKit/UIKit.h>

@interface BIDBusinessSelectViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSArray *linkedBusinessParseArray;
//Stores the array of models
@property (strong, nonatomic) NSMutableArray *linkedBusinessParseModelArray;
@property NSUInteger relevantIndex;
@property (strong, nonatomic) UIImage *tempImageHolder;

@end

#import "BIDBusinessSelectViewController.h"
#import <Parse/Parse.h>
#import "BIDBusinessModel.h"
#import "BIDSelectBusinessCustomCell.h"

@interface BIDBusinessSelectViewController () <ImageSelect>
{
    BIDSelectBusinessCustomCell *aCell;//define a cell of ur custom cell to hold selected cell
    UIImage *choosenImage; //image to set the selected image
}
@end

@implementation BIDBusinessSelectViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    self.linkedBusinessParseModelArray = [[NSMutableArray alloc]init];

    //create query
    PFQuery *linkedBusinessParseQuery = [PFQuery queryWithClassName:@"linkedBusinessParseClass"];
    //follow relationship
    [linkedBusinessParseQuery whereKey:@"currentBusinessUserParse" equalTo:[PFUser currentUser]];
    [linkedBusinessParseQuery whereKey:@"linkRequestSentParse" equalTo:@"Approved"];
    [linkedBusinessParseQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.linkedBusinessParseArray = objects; //store them in my model array
        //loop through model array from Parse
        for (NSDictionary *dict in self.linkedBusinessParseArray) {
            NSString *descPlaceNameParse = [dict objectForKey:@"placeDescriptionParse"];
            NSLog(@"descPlacesNameParse: %@",descPlaceNameParse);
            PFObject *tempObj = (PFObject *) dict;
            NSString *tempObjString = tempObj.objectId;
            NSLog(@"tempObjString (inside dict): %@", tempObjString);

            //storing values from Parse into my model
            BIDBusinessModel *userModel = [[BIDBusinessModel alloc]init];
            userModel.descriptionModelParse = descPlaceNameParse;
            userModel.objectIdModelParse = tempObjString;
            [self.linkedBusinessParseModelArray addObject:userModel];
            NSLog(@"self.linkedBusinessParseModelArray: %lu", (unsigned long)[self.linkedBusinessParseModelArray count]);
            //Reload tableview. Has to go here in block otherwise it does not occur
            [self.myTableView reloadData];

        }

    }];


    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];

        [myAlertView show];

    }

    choosenImage = [UIImage imageNamed:@"pin.png"]; //hear u need to set the image for cell assuming that u are setting initially same image for all the cell


}

#pragma mark -
#pragma mark Table Delegate Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.linkedBusinessParseModelArray.count; //returns count of model NSMutableArray
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Start cellforRowStIndex");
    static NSString *CellIdentifier = @"Cell";
    BIDSelectBusinessCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[BIDSelectBusinessCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    BIDBusinessModel *bizModel;
    bizModel = self.linkedBusinessParseModelArray[indexPath.row];
    bizModel.image = choosenImage;
    //cell.descLabel.text = [NSString stringWithFormat:@"bid= %d",indexPath.row];//set text from the model//Omitted for my desc
    cell.descLabel.text = bizModel.descriptionModelParse;

    cell.logoImage.image =bizModel.image; //setting the image initially the image when u set in "viewDidLoad" method from second time onwords it will set from the picker delegate method
    //insted of settig the gesture hear set it on the custom cell
    cell.ImageSelectDelegate = self; //setting the delegate
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

// hear implementation of delegate method

- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
{
    //open up the image picker
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    aCell = (BIDSelectBusinessCustomCell *)logoImgView.superview; //her getting the cell

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


}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *aChosenImage = info[UIImagePickerControllerEditedImage];
    //UIImage holder to transfer to cellForRowAtIndexPath
    choosenImage = aChosenImage;
    NSIndexPath *indexPath = [self.myTableView indexPathForCell:aCell];

    [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //hear reloading the selected cell only not entire tableview
    //get the model and set the choosen image
    BIDBusinessModel *bizModel;
    bizModel = self.linkedBusinessParseModelArray[indexPath.row];
    bizModel.image = aChosenImage;


    [picker dismissViewControllerAnimated:YES completion:NULL];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

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

@end

#import <UIKit/UIKit.h>

@protocol ImageSelect<NSObject> //for this u need use custom delegate so i did like this
- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
@end


@interface BIDSelectBusinessCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *descLabel;
@property (strong, nonatomic) IBOutlet UIImageView *logoImage;
@property (nonatomic, assign) id<ImageSelect>ImageSelectDelegate; // deleagte

@end

#import "BIDSelectBusinessCustomCell.h"

@implementation BIDSelectBusinessCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.logoImage = [[UIImageView alloc]init];
        self.descLabel = [[UILabel alloc]init];
        //set up gesture hear in the custom cell insted in the controller class
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLogoImage:)];
        [tap setNumberOfTouchesRequired:1];
        [tap setNumberOfTapsRequired:1];
        [tap setDelegate:self];
        self.logoImage.userInteractionEnabled = YES;
        [self.logoImage addGestureRecognizer:tap];

        //[self addSubview:logoImage];
        //[self addSubview:descLabel];

    }
    return self;}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)tapLogoImage:(UITapGestureRecognizer *)guesture
{
    if([self.ImageSelectDelegate respondsToSelector:@selector(selectSetImageForSelectedLogImage:)])
    {
        [self.ImageSelectDelegate selectSetImageForSelectedLogImage:self.logoImage];//call the delegate method from the selected cell
    }

}

@end
4

2 に答える 2

0

はい、これはすべての行に画像を設定するのは本当です..

今あなたができることは次のとおりです: -

tableViewCells のロード中に、UIImageView のタグを (indexPath.Row) として割り当てるだけです。

これにより、一意のタグが ImageView に割り当てられます。タップ中にその imageView のタグを簡単に取得でき、そのimageView の tagValue を介して特定の imageView に画像を割り当てることができるようになりました。

すべてが一意のタグを持っているため、これにより特定の imageView に画像が割り当てられます。

問題の答えが得られたことを願っています。

于 2013-09-30T09:30:38.863 に答える