I have a custom cell loaded from my table, with an image and a label the label shows ok but the image doesn't show
CustomCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: (NSString*)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self initLabels];
CGRect vintageScreenRect = CGRectMake(25, 0.0f, 100, 100);
self.iconImage = [[UIImage alloc]init];
UIImageView *vintageScreen = [[UIImageView alloc] initWithFrame:vintageScreenRect];
//[vintageScreen setImage:[UIImage imageNamed:#"vidButtonImg.png"]];
// [vintageScreen setImage:self.iconImage];
[vintageScreen setImage:[UIImage imageNamed:self.tingo]];
vintageScreen.opaque = YES; // explicitly opaque for performance
[self.contentView addSubview:vintageScreen];
[vintageScreen release];
NSLog(#"tingo ::%#", self.tingo);
}
return self;
}
UsingTable.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.tingo = [NSString stringWithFormat:#"picsButtonImg.png"];
cell.iconName.text = #"d";
}
return cell;
}
So the label shows ok , but the image doesn't show, I have tried with sending an uiImage and an NSString,
what is missing? thanks!
self.tingo probably is nil in initWithStyle method. You are asigning self.tingo property after initWithStyle call.
That cause [UIImage imageNamed:self.tingo] is also nil, and the image simply doesn't exist.
You can fix it for example by custom setter of tingo property, or by making vintageScreen as property and set image "from outside".
Your variable tingo is null when you first instantiate the cell. Doing cell.tingo = [NSString stringWithFormat:#"picsButtonImg.png"]; sets it to a value, but then your cell never knows to reset the image.
Move vintageScreen into a variable on your cell class, then override your tingo setter to reload the image if you want to keep the code you have in cellForRowAtIndexPath:
CustomCell.h
#interface CustomCell : UITableViewCell {
UIImageView *vintageImage;
NSString *tingo;
}
#property (nonatomic, retain) UIImageView *vintageImage;
#property (nonatomic, retain) NSString *tingo;
#end
CustomCell.m
#implementation CustomCell
#synthesize vintageImage, tingo;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initLabels];
[self addSubview:[self vintageScreen]];
}
return self;
}
-(UIImageView *)vintageScreen {
if(vintageScreen == nil) {
CGRect vintageScreenRect = CGRectMake(25, 0.0f, 100, 100);
[self setVintageScreen:[[[UIImageView alloc] initWithFrame:vintageScreenRect] autorelease]];
[vintageScreen setImage:[UIImage imageNamed:self.tingo]];
vintageScreen.opaque = YES;
}
return vintageScreen;
}
-(void)setTingo:(NSString *)newTingo {
[tingo release];
tingo = [newTingo retain];
[[self vintageScreen] setImage:[UIImage imageNamed:tingo]];
}
Related
When I click the UITableViewCell , It have selection effect (grey background in clicked cell),But didSelectRowAtIndexPath is not calling ,what happen?
EDIT
this is my code
tableView.h file
#interface PopCardView : MMPopupView <UITableViewDataSource, UITableViewDelegate>
#end
tableView.m file
#property (nonatomic, strong) NSMutableArray *tagsArray;
#property (nonatomic, strong) UIView *backView;
#property (nonatomic, strong) UITableView *tableView;
#property (nonatomic, assign) NSUInteger lastIndex;
#end
-(id)initWithTags:(NSMutableArray *)tags{
self = [super init];
if (self) {
self.backView = [[UIView alloc] init];
self.backView.backgroundColor = [UIColor whiteColor];
self.backView.layer.cornerRadius = 5;
self.backView.layer.masksToBounds = YES;
[self addSubview:self.backView];
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.right.equalTo(self);
}];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 324, 300) style:UITableViewStylePlain];
_tableView.tableFooterView =[[UIView alloc] init];
[self.backView addSubview:_tableView];
[_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.bottom.equalTo(self.backView).insets(UIEdgeInsetsMake(45,0, 45, 15));
make.size.mas_equalTo(CGSizeMake(324, 200));
}];
[_tableView registerClass:[PopCardTagViewCell class] forCellReuseIdentifier:#"cell"];
_tableView.allowsSelection = YES;
_tableView.allowsSelectionDuringEditing = YES;
[_tableView setUserInteractionEnabled:YES];
_tableView.dataSource = self;
_tableView.delegate = self;
}
return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_tagsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PopCardData *data = (PopCardData *)obj;
data.selected = #"0";
if (idx == indexPath.row) {
data.selected = #"1";
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifer = #"cell";
PopCardTagViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifer];
if (!cell) {
cell = [[PopCardTagViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}
[self configureCell:cell forIndexPath:indexPath];
return cell;
}
-(void)configureCell:(PopCardTagViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
PopCardData *data = (PopCardData *)[_tagsArray objectAtIndex:indexPath.row];
//configure cell
[cell setUserInteractionEnabled:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tagsArray.count;
}
EDIT2
this is my initialize code of PopCardView,it use swift
let pop = PopCardView(tags: self.m_model.getItmes())
pop.show()
The code you show does not set any delegate to the table view. Either this is the reason or you posted an incomplete code snippet.
self.tableView.delegate = self;
and add UITableViewDelegate to your interface like
#interface ClassName ()<UITableViewDelegate>
Make sure you don't have anything in the cell that can swallow the touch event. Things like buttons and textfields can cause this. Strip everything from your cell, test to see if it works, then add things back in slowly to find the culprit.
I have created a custom tableCell When I connect the xib via backgroundView to the file owner I successfully get items to appear in my table. The problem is I am not able to select a row and get a return.
If I unselect backgroundView then my custom cell does not populate but I am able to select a cell and extract a notification. I obviously want both of these items to work. Display my custom cell and have the ability to select a specific cell.
GameTableCell.m
#import "GameTableCell.h"
#implementation GameTableCell
#synthesize GameTime, AwayImage, HomeImage;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
*/
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"GameTableCell" owner:self options:nil];
self = [topLevelObjects objectAtIndex:0];
return self;
}
#end
GameTable.h:
#import <UIKit/UIKit.h>
#interface GameTableCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *GameTime;
#property (weak, nonatomic) IBOutlet UIImageView *AwayImage;
#property (weak, nonatomic) IBOutlet UIImageView *HomeImage;
#end
HomePage.m:
#import "HomePage.h"
#import "PickSport.h"
#import "GameTableCell.h"
static NSString *CellIdentifier = #"GameTableCell";
NSString *headerText;
#interface HomePage ()
#end
#implementation HomePage
#synthesize ActiveChal,PendingChal;
-(IBAction)makeChallenge:(id)sender{
PickSport *second = [[PickSport alloc] initWithNibName:#"PickSport" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Arrays for filling table cells
_HomeImages=#[#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl", ];
_AwayImages=#[#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl", ];
_SportGameInfo=#[#"7:00pm",#"8:00pm",#"9:00pm",#"10:00pm",];
[self.PendingChal registerClass:[GameTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.ActiveChal registerClass:[GameTableCell class] forCellReuseIdentifier:CellIdentifier];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//Set up the table props
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//number of rows in the table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// rows eqiv to length of array SportGameinfo
return _SportGameInfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=#"GameTableCell";
GameTableCell *cell = (GameTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(tableView == PendingChal){
NSLog(#"in Pend table");
int row =[indexPath row];
cell.HomeImage.image=[UIImage imageNamed:[_HomeImages objectAtIndex:indexPath.row]]; //thumbnails objectAtIndex:indexPath.row]
cell.AwayImage.image=[UIImage imageNamed:[_AwayImages objectAtIndex:indexPath.row]];
cell.GameTime.text=[_SportGameInfo objectAtIndex:indexPath.row];
}
if(tableView == ActiveChal){
NSLog(#"in Active table");
int row =[indexPath row];
cell.GameTime.text=_SportGameInfo[row];
cell.HomeImage.image=[UIImage imageNamed:_HomeImages[row]];
cell.AwayImage.image=[UIImage imageNamed:_AwayImages[row]];
}
return cell;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 1. The view for the header
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
if(tableView == PendingChal){
headerText= #"Pending Challenges (waiting on you)";
}
else {
headerText= #"Active Challenges";
}
// 2. Set a custom background color and a border
headerView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
headerView.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:1.0f].CGColor;
headerView.layer.borderWidth = 0;
// 3. Add a label
UILabel* headerLabel = [[UILabel alloc] init];
headerLabel.frame = CGRectMake(0, 0, tableView.frame.size.width -0, 20);
headerLabel.backgroundColor = [UIColor blackColor];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16.0];
headerLabel.text = headerText;
headerLabel.textAlignment = NSTextAlignmentLeft;
// 4. Add the label to the header view
[headerView addSubview:headerLabel];
// 5. Finally return
return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath");
/*UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:#"You've selected a row" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];*/
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:[_SportGameInfo objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display the Hello World Message
[messageAlert show];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"willSelectRowAtIndexPath");
return indexPath;
}
#end
HomePage.h:
#import <UIKit/UIKit.h>
#interface HomePage : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *PendingChal;
#property (strong, nonatomic) IBOutlet UITableView *ActiveChal;
#property (nonatomic, strong)NSArray *HomeImages;
#property (nonatomic, strong)NSArray *AwayImages;
#property (nonatomic, strong)NSArray *SportGameInfo;
#end
And to be complete here are the connection of my HomePage.xib
Put aside your app's weird behavior I notice you are not rendering the table cell in the usual way. So first try to render it in usual way as this :
register nib in viewDidLoad because you create your cell in xib
- (void)viewDidLoad {
[super viewDidLoad];
// Arrays for filling table cells
_HomeImages=#[#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl", ];
_AwayImages=#[#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl", ];
_SportGameInfo=#[#"7:00pm",#"8:00pm",#"9:00pm",#"10:00pm",];
[self.PendingChal registerNib:[GameTableCell nib] forCellReuseIdentifier:CellIdentifier];
[self.ActiveChal registerNib:[GameTableCell nib] forCellReuseIdentifier:CellIdentifier];
}
dequeue reusable cell in - tableView:cellForRowAtIndexPath: and return it
you don't need load nib in tableViewCell's init method remove
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"GameTableCell" owner:self options:nil];
self = [topLevelObjects objectAtIndex:0];
I added UIImageView to CustomCell(subclass of UITableViewCell).
But, when I set an image on imageView on CustomCell,
the app clash with log message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomCell setImageView:]: unrecognized selector sent to instance 0x10912d990'
I have this code.
ViewController
#property (nonatomic, strong) UITableView* tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 508)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifer = #"Cell";
CustomCell* cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifer];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
}
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"hello.jpg"];
break;
default:
break;
}
default:
break;
}
return cell;
}
CustomCell
#property (nonatomic, strong) UIImageView* imageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 310, 170)];
[self addSubview:self.imageView];
}
return self;
}
Do you have any idea to add an image to imageView on customCell?
Thank you.
What you're trying to do is to set your UITableViewCell's superclass readonly imageView property with this line self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 310, 170)];. You can't do that.
documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/imageView
#property (nonatomic, strong) UIImageView* imageView; should be in CustomCell.h not CustomCell.m so it's accessible from the other classes.
Thank you for many answers.I've resolved.
#property (nonatomic, strong) UIImageView* imageView;
↓
#property (nonatomic, strong) UIImageView* myImageView;
i think you have to do like something this...
in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==TABLE_MEMBERS)// && (count_cell%2==0))
{
Groupie_Moment_Timeline_Cell_Members *cell_mem =[tableView dequeueReusableCellWithIdentifier:#"Groupie_Moment_Timeline_Cell_Members"];
if (cell_mem==nil)
{
NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:#"Groupie_Moment_Timeline_Cell_Members" owner:self options:nil
];
cell_mem=[nibs objectAtIndex:0];
}
cell_mem.selectionStyle=UITableViewCellSelectionStyleNone;
[cell_small.IMG_TableImage setImage:[UIImage imageNamed:#"a.png"];
return cell_small;
I would like to programmatically add two text fields to an tableViewCell sub-class (more later) but having difficultly accessing the text-field from the view controller.
My table view sub class is:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
textField = [[UITextField alloc] initWithFrame:CGRectZero];
textField.clearsOnBeginEditing = NO;
textField.textAlignment = UITextAutocapitalizationTypeAllCharacters;
textField.returnKeyType = UIReturnKeyDone;
[self.contentView addSubview:textField];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect rect2 = CGRectMake(100.0, 10.0, 200, 30.0);
textField.text = #"testing";
[textField setFrame:rect2];
}
Within my view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
XTSessionCell_iPad *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[XTSessionCell_iPad alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
// ????
// cell.textField.text = #"enter";
return cell;
}
Any help very much appricatated.
Thanks
As others have mentioned in the comments, make the textView a readonly property in your XTSessionCell_iPad.h file. At a minimum your XTSessionCell_iPad.h file would look like:
#interface XTSessionCell_iPad : UITableViewCell
#property (nonatomic, readonly) UITextField *textField;
#end
And your XTSessionCell_iPad.m file would have a class extension to allow writing to that property:
#interface XTSessionCell_iPad()
#property (nonatomic, readwrite) UITextField *textField;
#end
Currently I have custom UITablViewCell's (BIDSelectBusinessCustomCell) that are displayed in a tableView (myTableView). The custom cell is made up of a UIImageView and a label. When the view loads the labels are populated with the strings from my model. UIImageViews are blank. I want to be able for a user to 'tap' the UIImageView, select a picture from what is stored on their phone and for that image to be saved to the UIImageView.
From the below code I can get the 'tap' gesture, then the pickercontroller pops up and a user selects an image. The way the code is now one image that is selected is set for all of the UIImageViews. Which is understandable. But I want it to be set to that particular UIImageView and don't know how to.
Code:
#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
Yup for this, i tried but finally i did something like this, go through this u may find some solution. and hope this helps u :)
//in custom cell "BIDSelectBusinessCustomCell.h"
#import <UIKit/UIKit.h>
//define a delegate method that is called each time when u tapped on cell
#protocol ImageSelect<NSObject> //for this u need use custom delegate so i did like this
- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
#end
#interface BIDSelectBusinessCustomCell : UITableViewCell <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView *logoImage; //your image holder
UILabel *descLabel; //label
}
#property (nonatomic, retain) UIImageView *logoImage;
#property (nonatomic, retain) UILabel *descLabel;
#property (nonatomic, assign) id<ImageSelect>ImageSelectDelegate; // deleagte
#end
//in custom cell "BIDSelectBusinessCustomCell.m"
#import "BIDSelectBusinessCustomCell.h"
#implementation BIDSelectBusinessCustomCell
#synthesize logoImage;
#synthesize descLabel;
#synthesize ImageSelectDelegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
logoImage = [[UIImageView alloc]init];
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];
logoImage.userInteractionEnabled = YES;
[logoImage addGestureRecognizer:tap];
[self addSubview:logoImage];
[self addSubview:descLabel];
}
return self;
}
- (void)dealloc
{
[logoImage release];
[descLabel release];
[super dealloc];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
//in this method simply i am setting the frames of subviews
[super layoutSubviews];
self.logoImage.frame = CGRectMake(130, 2, 44,44 );
self.descLabel.frame = CGRectMake(20, 2, 60, 44);
}
- (void)tapLogoImage:(UITapGestureRecognizer *)guesture
{
if([self.ImageSelectDelegate respondsToSelector:#selector(selectSetImageForSelectedLogImage:)])
{
[self.ImageSelectDelegate selectSetImageForSelectedLogImage:self.logoImage];//call the delegate method from the selected cell
}
}
#end
//in your view controller class u need to do something like this ;)
//ViewController.h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (retain, nonatomic) IBOutlet UITableView *aTableView;
#property (nonatomic, retain) NSIndexPath *selectedIndexPath; //no need just try
#end
//in your "ViewController.m" file
#interface ViewController ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControll erDelegate,ImageSelect>//delegate
{
BIDSelectBusinessCustomCell *aCell;//define a cell of ur custom cell to hold selected cell
UIImage *choosenImage; //image to set the selected image
}
- (void)viewDidLoad
{
[super viewDidLoad];
choosenImage = [UIImage imageNamed:#"Star.png"]; //hear u need to set the image for cell assuming that u are setting initially same image for all the cell
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[aTableView release];
[super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4; //pass array check array contains valid objects
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BIDSelectBusinessCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil)
{
cell = [[BIDSelectBusinessCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
BIDBusinessModel *bizModel;
bizModel = self.linkedBusinessParseModelArray[indexPath.row];
bizModel.image = aChosenImage;
cell.descLabel.text = [NSString stringWithFormat:#"bid= %d",indexPath.row];//set text from the model
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 = [aTableView indexPathForCell:aCell];
[self.aTableView 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];
}
Yes it is true this would set the image to every row..
Now what you can do is : -
While loading of tableViewCells just assign the tag of UIImageView as the (indexPath.Row).
This would assign unique tags to the ImageView.
You can easily get the tag of that imageView while tapping and now you can assign the image to particular imageView through tagValue of that imageView.
This would assign image to a particular imageView as all have unique tags.
Hope you got the answer of your problem.