Reloading images in the buttons present in the table view - objective-c

In my table view I have 4 UIButtons, 2 in each row of table view. Initially all the buttons have an image on them (they're hardcoded). Now there is a button in my app which when tapped replaces the images on the buttons with new ones using the setImage method and then reloads the table view. But unfortunately I am getting the old table view only i mean with old images. How can I get the new images on the buttons?
My cellForRowAtIndexPath implementation:-// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button1 setImage:[UIImage imageNamed:#"BUTTON.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:self.button1];
button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button2 setImage:[UIImage imageNamed:#"TURN OFF.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
}
else if(indexPath.row == 1)
{
button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button3 setImage:[UIImage imageNamed:#"JUMP.png"] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button4 setImage:[UIImage imageNamed:#"CLOSE.png"] forState:UIControlStateNormal];
[button4 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button4];
}
else
{
// Do something here
}
return cell;
}

You are probably setting the image for the wrong state:
[myButton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
You need to set the correct value for UIControlStateNormal - where correct means the one that matches your buttons' current state(s).

It Seems that you have hardcoded images in cell for row method, so whenever you are reloading your tableview , they will have same images.
So If you like to change them then take a boolean varible and Update it like :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button1 setImage:[UIImage imageNamed:#"BUTTON.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:self.button1];
button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button2 setImage:[UIImage imageNamed:#"TURN OFF.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
}
else if(indexPath.row == 1)
{
button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button3 setImage:[UIImage imageNamed:#"JUMP.png"] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button4 setImage:[UIImage imageNamed:#"CLOSE.png"] forState:UIControlStateNormal];
[button4 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button4];
}
else
{
// Do something here
}
if(myflag)
{
[button1 setImage:[UIImage imageNamed:#"changedImage1"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"changedImage2"] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:#"changedImage3"] forState:UIControlStateNormal];
[button4 setImage:[UIImage imageNamed:#"changedImage4"] forState:UIControlStateNormal];
}
return cell;
}
// In Click to Method Set myflag yes and reload table
-(void)ClickTo:(IBAction )sender
{
myflag=YES;
[tableview reloadData];
}

Related

Reload Section does not handle properly

I have the following implementation where I have title, and two buttons(titles are half and full) on the tablecell. When user selects and close the section and open it again, he only sees the default values of button title which is full rather than his selection.
I could able to see the following method (setHalfButton :indexPath
) is getting called during the reloading, but it does not have any effect.
Code and screenshots are follows.
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath setQuantity :(double) quantity {
NSMutableArray* array = [selectedRowsInSectionDictionary objectForKey:#(indexPath.section)];
if(array){
[array addObject:indexPath];
} else {
array = [NSMutableArray array];
[array addObject:indexPath];
[selectedRowsInSectionDictionary setObject:array forKey:#(indexPath.section)];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ComboCell";
ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[ComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if([selectedRowsInSectionDictionary[#(indexPath.section)] containsObject: indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if(
comboItemsArray[indexPath.section].allComboItems[indexPath.row].pQuantity == 0.5)
{
// it comes here after reloading
[self setHalfButton:indexPath];
}
else
{
[self setFullButton:indexPath];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.halfBtnOutlet.hidden = YES;
cell.fullBtnOutlet.hidden = YES;
}
cell.comboTitle.text =comboItemsArray[indexPath.section].allComboItems[indexPath.row].pName;
[cell.halfBtnOutlet addTarget:self action:#selector(halfBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.fullBtnOutlet addTarget:self action:#selector(fullBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void) setHalfButton : (NSIndexPath*)indexPath
{
ComboTableViewCell* cell = [comboTableView cellForRowAtIndexPath:indexPath];
[cell.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[cell.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[cell.halfBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[cell.fullBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
-(void) setFullButton : (NSIndexPath*)indexPath
{
ComboTableViewCell* cell = [comboTableView cellForRowAtIndexPath:indexPath];
[cell.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[cell.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[cell.fullBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[cell.halfBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
Your setHalfButton and setFullButton functions are being called from the cellForRowAtIndexPath: datasource method, but they are calling the cellForRowAtIndexPath: tableview method. Since you have not yet returned the cell from the former method, the latter method will return nil to cell, resulting in no visible update.
The setHalfButton and setFullButton methods should be in your ComboTableViewCell class:
-(void) setHalfButton
{
[self.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[self.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[self.halfBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[self.fullBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
-(void) setFullButton
{
[self.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[self.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[self.fullBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[self.halfBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
Also, you are adding the button action handlers each time you dequeue a cell, but you should only do this when you allocate a new cell. From a design point-of-view these button tap handlers should also be in your ComboTableViewCell class with a delegation pattern to notify the view controller that the half/full was changed.
At the very least it should look something like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ComboCell";
ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[ComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell.halfBtnOutlet addTarget:self action:#selector(halfBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.fullBtnOutlet addTarget:self action:#selector(fullBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
}
if([selectedRowsInSectionDictionary[#(indexPath.section)] containsObject: indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if(
comboItemsArray[indexPath.section].allComboItems[indexPath.row].pQuantity == 0.5)
{
// it comes here after reloading
[cell setHalfButton];
}
else
{
[cell setFullButton];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.halfBtnOutlet.hidden = YES;
cell.fullBtnOutlet.hidden = YES;
}
cell.comboTitle.text =comboItemsArray[indexPath.section].allComboItems[indexPath.row].pName;
return cell;
}

accesing contents of uitableview cell in another class

i have a class loginViewController which has table contents. i have loaded a custom cell to each row of the table. in method cellForRowAtIndexPath i have created a button for each row and asigned it a tag value. clicking on this button takes us to new view controller logininsidesViewController. Now in logininsidesViewController class i want to access the button tag i created earlier.what should i do?
loginViewController.m class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
customcell *cell=(customcell*)[tableView dequeueReusableCellWithIdentifier:#"cellcstm"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(customActionPressed)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
[cell addSubview:button];
if (cell==nil)
{
//[tableView registerNib:[UINib nibWithNibName:#"customcell" bundle:nil] forCellReuseIdentifier:#"cellcstm"];
NSArray *arr12=[[NSBundle mainBundle]loadNibNamed:#"customcell" owner:self options:nil];
cell=[arr12 objectAtIndex:0];
}
for (int i=0; i<=indexPath.row; i++)
{
cell.selectionStyle=UITableViewCellEditingStyleNone;
}
if (indexPath.row==0)
{
cell.lbl.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if (indexPath.row==1)
{
button.tag=1;
cell.img.image=[UIImage imageNamed:#"facebook.png"];
cell.lbl.text=#"Facebook";
[cell.contentView addSubview:button];
}
if (indexPath.row==2)
{
button.tag=2;
cell.img.image=[UIImage imageNamed:#"g+.png"];
cell.lbl.text=#"Google";
[cell.contentView addSubview:button];
}
if (indexPath.row==3)
{
button.tag=3;
cell.img.image=[UIImage imageNamed:#"youtube.png"];
cell.lbl.text=#"Youtube";
[cell.contentView addSubview:button];
}
if (indexPath.row==4)
{
button.tag=4;
cell.img.image=[UIImage imageNamed:#"twitter.png"];
cell.lbl.text=#"Twitter";
[cell.contentView addSubview:button];
}
if (indexPath.row==5)
{
button.tag=5;
cell.img.image=[UIImage imageNamed:#"flicker.png"];
cell.lbl.text=#"Flicker";
[cell.contentView addSubview:button];
}
else
{
cell .textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}
i want to access the button.tag in my logininsidesViewController class. plz help
In continuation with Viruss's answer you can create a property with integer type in logininsidesViewController class and pass that from button click.
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
logininsidesViewController *objLoginInside = [[logininsidesViewController alloc] initWithNibName:#"logininsidesViewController" bundle:nil];
objLoginInside.buttonTag = sender.tag;
[self.navigationController pushViewController:objLoginInside animated:NO];
}
Add One target method to your buttons and access it with tags,
In cellForRow method Update to this.
[button addTarget:self action:#selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
You can access here with tag,
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
}
You are adding your button on cell and again you're adding it on cell's contentView correct that is the problem.
If you want adding it in cell.contentView you must provide allocation in each if condition and then add it
Edit: Do smart coding
viewDidLoad()
[self.myTableView registerNib:[UINib nibWithNibName:#"CViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
arImage = #[#"facebook.png", #"g+.png", #"twitter.png", #"flicker.png" ];
arLables = #[#"Facebook", #"GooglePlus", #"Tweeter", #"Flicker"];
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
if (indexPath.row==0)
{
cell.lblText1.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if ( indexPath.row > 0 && indexPath.row <= 5) {
button.tag = indexPath.row;
cell.imageView.image=[UIImage imageNamed:arImage[indexPath.row - 1]];
cell.textLabel.text=[arLables objectAtIndex:indexPath.row - 1];
[cell.contentView addSubview:button];
} else {
cell.textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}

how to maintain state of uibutton in table view

I have a table view in that I am using a custom cell. I have two buttons on each table view cell named "edit" and "cancel" both have images in cellForRow. What I want that when user clicks on edit button at same time same rows cancel button should change its image. Code is Working but it not changing same rows cancel button image. Its changing another rows cancel button image. How to maintain state of each button.
Here Is Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell== nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
// Creating Label Menu Cost
_amountMenu = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
//Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton setTag:indexPath.row];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// Creating Button For Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
// Adding All To Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
//objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );
return cell;
}
-(void)editQuantity:(id)sender{
NSLog(#"count of the array is----%d",[imageViewArray count]);
button = (UIButton *)sender;
row = button.tag;
NSLog(#"---rowww%d",row);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:#"edit_over.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:#"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
_cancelButton.tag = 0;
}
-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(#"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag == 0){
_textFieldQuantity.userInteractionEnabled = NO;
UIImage *buttonImageCancel = [UIImage imageNamed:#"check_over.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
UIImage *buttonImageCancel1 = [UIImage imageNamed:#"cancel.png"];
[_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"edit.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
_cancelButton.tag = 1;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iHomeDelivery" message:#"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Try to make _cancelButton and _checkButton with different tag in the same cell, I think it is puzzle to make both of them with same tag in the same row.
Give different tag for each buttons first like :
[_checkButton setTag:indexPath.row];
[_cancelButton setTag:indexPath.row+100];
and in your editQuantity: method
button = (UIButton *)sender;
row = button.tag;
NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:currentIndexPath];
newCancelButton = (UIButton *)[newCell.contentView viewWithTag:row+100];
[newCancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
Do same in your cancelOreder: method to change image of _checkButton.
Also, use this for remove all subViews,
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
please try this solution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL_ID"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL_ID"];
UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_cancelButton setFrame:CGRectMake(65, 13, 100, 28)];
[_cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton setTag:1];
UIButton *_checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_checkButton setFrame:CGRectMake(200, 13, 100, 28)];
[_checkButton setTitle:#"Check" forState:UIControlStateNormal];
[_checkButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
[_checkButton setTag:2];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
}
return cell;
}
-(void) cancelOreder:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
UIButton *btn = (UIButton *)[cell viewWithTag:2];
if(btn != nil)
{
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
-(void) editQuantity:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
UIButton *btn = (UIButton *)[cell viewWithTag:1];
if(btn != nil)
{
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
in this example i cheange text color of label you can change image,hope this will help you

How to pass tableview cell as an argument and load the images only to the corresponding tableview cells she photo button is clicked in iOS

i ve got a custom tableview .i want to pass the tableview cell as a pramater to a function.I m using [cell.btnImages performSelector].while clicking the photos button it should load the images to the corresponding tableview cell .is it possible ?below is the code.when image button is clicked .i need to pass the cell as an argument...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"IpadTableViewCell";
IPadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"IpadTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[IPadTableViewCell class]])
{
cell = (IPadTableViewCell *)currentObject;
}
}
}
[cell.btnImages performSelector:#selector(imageButtonClicked:) onThread:nil withObject:cell waitUntilDone:nil];
return cell;
}
-(void)imageButtonClicked:(IPadTableViewCell *)tblCell
{
// opne image select dialog
CGFloat prevButtonXPosition = 17, pageButtonXPosition, pageButtonWidth = 120, pageButtonHeight = 130;
for (NSInteger i =1; i<= 9; i++) {
pageButtonXPosition = prevButtonXPosition ;
prevButtonXPosition = pageButtonXPosition + pageButtonWidth;
UIButton *pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pageButton addTarget:self action:#selector(onClickImage:) forControlEvents:UIControlEventTouchUpInside];
[pageButton setFrame:CGRectMake(pageButtonXPosition, 5, pageButtonWidth, pageButtonHeight)];
[pageButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%i.png",i]] forState:UIControlStateNormal];
[pageButton setTag:i];
[cell.scrollView addSubview:pageButton];
}
UIButton *add=[UIButton buttonWithType:UIButtonTypeCustom];
[add addTarget:self action:#selector(onClickAdd:) forControlEvents:UIControlEventTouchUpInside];
[add setFrame:CGRectMake(prevButtonXPosition, 20, 80, 80)];
[add setBackgroundImage:[UIImage imageNamed:#"last.jpeg"] forState:UIControlStateNormal];
[add setTag:0];
[cell.scrollView addSubview:add];
[cell.scrollView setContentSize:CGSizeMake(prevButtonXPosition +80, 40)];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
You have defined the imageButtonClicked method in your tableview class but your calling for it in cell.btnImages. You should implement the imageButtonClicked method in the class of btnImages or call on it in self instead of cell.btnImages. If btnImages is of class UIButton you should instead do this:
[cell.btnImages addTarget:self action:#selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
Or simply just implement the UITableViewDelegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Issue With setting tag for Button UITableView

I have two buttons inside sectioned tableview cell thumbs up and thumbs down. Initially image of both button is not selected. When I select thumbs up button its image become thumbs up selected and other one become thumbsdown not selected and vice versa.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"mod:numberOfSectionsInTableView");
NSLog(#"[preferences count]=%d",[preferences count]);
return [preferences count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.choices count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
NSLog(#"cellForRowAtIndexPath: DISPLAY TEST");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"Text is: %#", [choices objectAtIndex:indexpath.row]);
NSLog(#"CHOICE AT INDEX PATH IS: %#",[choices objectAtIndex:indexpath.row %[choices count]]);
cell.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
// Thumbs up button.
//UIButton *thumbsUp = [[UIButton alloc]init];
thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
[thumbsUp addTarget:self action:#selector(thumbUp_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsUp setTitle:#"" forState:UIControlStateNormal];
thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
[thumbsUp setBackgroundImage:[UIImage imageNamed:#"thumbsup_not_selected.png"]
forState:UIControlStateNormal];
//NSLog(#"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
[cell.contentView addSubview:thumbsUp];
// Thumbs down button
thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsDown addTarget:self action:#selector(thumbDown_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsDown setTitle:#"" forState:UIControlStateNormal];
thumbsDown.frame = CGRectMake(200, 20, 20, 15);
[thumbsDown setTag:indexpath.row+120];
[cell.contentView addSubview:thumbsDown];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[thumbsDown setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]
forState:UIControlStateNormal];
}
NSLog(#"------------> TAG TEST %d",thumbsUp.tag);
cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];
NSLog(#"HELLO FOR TESTING");
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(15, 10, 300, 25);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = UITextAlignmentLeft;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
[view autorelease];
[view addSubview:label];
//[view addSubview:segmentedControl];
view.backgroundColor = [UIColor grayColor];
return view;
}
//Thumbs Up Button Action
- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
{
NSLog(#"Thumbs Up Check!");
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
NSLog(#"indexpath =%d",indexPath.row);
//[button setTag:indexPath.row+(indexPath.section * 50)];
int cTag = [sender tag];
NSLog(#"------>TAG : %d", cTag);
NSLog(#"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
if(cTag == (indexPath.row+(indexPath.section * 50)))
{
NSLog(#"BUTTON COUNT:");
[button setBackgroundImage:[UIImage imageNamed:#"thumbsup_selected.png"]
forState:UIControlStateNormal];
}
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
//int row = button.tag;
NSLog(#"SECTION IS:%d",section);
NSLog(#"ROW IS: %d",row);
NSArray *array = cell.contentView.subviews;
NSLog(#"NUMBER OF OBJECTS: %d",[array count]);
UIButton *test = (UIButton *)[array objectAtIndex:2];
[test setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
}
Due to issue with tag of button while I change image of one button several buttons are changing. If any one can please find a solution it will be helpful.... tag is setting for buttons in sections which we can view.
The reason is the bad use of the recycling/reuse mechanism (as with 75% of questions about UITableView…)
Go read the Table View Programming Guide in Apple's doc (and search SO and the web for any question related to tableview and the reuse mechanism)
Corrected the issue by creating buttons outside and inside if(cell == nil). Also created a mutable dictionary to keep the current state of the button.....