Following is a code fragment from cellForRowAtIndexPath. I have added 2 buttons on a Cell.
When the user clicks on Button A the image of Button A should be a.png. And Button B should change to default.png.
When the user clicks on Button B the image of Button B should be b.png. And Button A should change to default.png.
How can i do this ?
cellForRowAtIndexPath Method
[cell.aButton addTarget:self action:#selector(methodA:) forControlEvents:UIControlEventTouchUpInside];
[cell.bButton addTarget:self action:#selector(methodB:) forControlEvents:UIControlEventTouchUpInside];
- (void) methodA:(id) sender {
[((UIButton *)sender) setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
// How to change the image of Button B to default.png ??
}
- (void) methodB:(id) sender {
[((UIButton *)sender) setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
// How to change the image of Button A to default.png ??
}
If you have set tag to your buttons like aButton has tag= 1, and bButton has tag= 2
- (void) methodB:(id) sender {
UIButton *bButton =((UIButton *)sender) ;
[bButton setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
UIButton *aButton = (UIButton *)[bButton.superview viewWithTag:bButton.tag-1];
[aButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateSelected];
}
- (void) methodA:(id) sender {
UIButton *aButton =((UIButton *)sender) ;
[aButton setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
UIButton *bButton = (UIButton *)[bButton.superview viewWithTag:aButton.tag+1]; //updated here, instead of bButton, put aButton
[bButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateSelected];
}
Enjoy Coding !!
Try this,
- (void) methodA:(id) sender {
UIButton *buttonA = (UIButton *)sender;
CGPoint pointInTable = [field convertPoint:buttonA.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:nowIndex];
//now access the button and change button images
//cell.aButton setImage:
//cell.bButton setImage:
}
Do the same for button B action.
Note: I recommend using custom tableview cell and handling this button actions inside the custom cell.
For both the buttons, set a.png and default.png for UIControlStateNormal and UIControlStateSelected respectively
Now you can simply make the button selected and deselected accordingly and donot worry about the image.
Hope this image gives you another hint.
To handle your selection issue, the best way is to handle it in your tableViewCell class, where you can access both the buttons and write a delegate or callback if required.
Try this...
cellForRowAtIndexPath
[cell.aButton addTarget:self action:#selector(methodToggle:) forControlEvents:UIControlEventTouchUpInside];
[cell.bButton addTarget:self action:#selector(methodToggle:) forControlEvents:UIControlEventTouchUpInside];
[cell.aButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[cell.bButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
[cell.aButton setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
[cell.bButton setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
cell.aButton.tag == 111;
cell.aButton.tag == 222;
Just One method for both Buttons
- (void) methodToggle:(id) sender {
UIButton * btn = (UIButton *)sender;
if (btn.tag == 111)
{
UIButton * btnAnother = (UIButton *)[[btn superview] viewWithTag:222];
btnAnother.selected = NO;
}
if (btn.tag == 222)
{
UIButton * btnAnother = (UIButton *)[[btn superview] viewWithTag:111];
btnAnother.selected = NO;
}
btn.selected = YES;
}
Inside tableView:cellForRowAtIndexPath: add following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
[cell.aButton setTag:1];
[cell.aButton addTarget:self action:#selector(methodA:) forControlEvents:UIControlEventTouchUpInside];
[cell.aButton setTag:2];
[cell.bButton addTarget:self action:#selector(methodB:) forControlEvents:UIControlEventTouchUpInside];
}
Implement the button event handlers as-
- (void) methodA:(id) sender
{
UIView *view = [sender superView];
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateSelected];
UIButton *otherButton = [view viewWithTag:2];
[otherButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
}
- (void) methodB:(id) sender
{
UIView * view = [sender superView];
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
UIButton *otherButton = [view viewWithTag:1];
[otherButton setImage:[UIImage imageNamed:#"default.png"] forState:UIControlStateNormal];
}
Write a CustomCell class which has 2 buttons with target methods and delegate method to notify the table view when buttons clicked. Use this cell in the table view.
In target methods do the image changes(A/B) and fire the delegate method to notify the tableViewController.
You can keep indexpath as a property of cell and pass it with the delegate to tableView.(Set the indexpath for the cell in tableView's cellForRowAtIndexPath:)Then you can identify which cell's button was clicked and relevant data object can fetch from your data source.
CustomCell.m
- (IBAction)onButtonAClicked:(id)sender {
// change your images
[delegate cellButtonClicked:self.indexpath];
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.delegate = self;
}
cell.indexpath = indexPath;
}
//delegate method
-(void)cellButtonClicked:(NSIndexPath *)indexPath{
//get the related data object for the cell using indexPath from your data source
}
Related
I created the UITableView programatically, and in my UITableView have 3 rows in the UITableView section. I have a UIButton in 3 rows. Initially, all the buttons of three rows have gray color. If I select the button in the 2nd row, then the button in the 2nd row should be red and the all others gray and so on. Can any one suggest me.
In your cellForRowAtIndexPath
UIButton *button = [[UIButton alloc]initWithFrame:Your Frame];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = indexPath.section;
[cell.contentView addSubview:button];
Then write buttonClicked method
-(void)buttonClicked:(UIButton*)button
{
UIButton *but1;
UIButton *but2;
if (button.tag == 0)
{
but1 = (UIButton *)[self.view viewWithTag:1];
but2 = (UIButton *)[self.view viewWithTag:2];
[but1 setBackgroundColor:[UIColor grayColor]];
[but2 setBackgroundColor:[UIColor grayColor]];
[button setBackgroundColor:[UIColor redColor]];
}
if (button.tag == 1)
{
but1 = (UIButton *)[self.view viewWithTag:0];
but2 = (UIButton *)[self.view viewWithTag:2];
[but1 setBackgroundColor:[UIColor grayColor]];
[but2 setBackgroundColor:[UIColor grayColor]];
[button setBackgroundColor:[UIColor redColor]];
}
if (button.tag == 2)
{
but1 = (UIButton *)[self.view viewWithTag:0];
but2 = (UIButton *)[self.view viewWithTag:1];
[but1 setBackgroundColor:[UIColor grayColor]];
[but2 setBackgroundColor:[UIColor grayColor]];
[button setBackgroundColor:[UIColor redColor]];
}
}
Hope it helps.
Try this:
Create the button as mentioned by patil (look below for his answer) ,and try to give it a tag.
button.tag=indexpath.section;
You need to set target-action of each button.Write the following line in the button action method:
[button setTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
Then implement someMethod: like this:
- (void)someMethod:(UIButton *)sender {
if (sender.tag == 1) {
// do action for button with tag 1
//change color here
[button setBackgroundColor:[UIColor redColor]];
} else if (sender.tag == 2) {
// do action for button with tag 2
} // ... and so on
}
I'm not too fond of the multiple if cases as you would need to re-write a lot when your amount of sections / rows change.
This is a more generic answer but it might not be so easy as the others. It involves created a UIButton subclass and a UITableViewCell subclass.
Create a UIButton subclass with a property to store the NSIndexPath of the cell where the button will be shown on:
//
// IndexPathButton.h
//
//
// Created by You on 28/07/15.
//
//
#import <UIKit/UIKit.h>
#interface IndexPathButton : UIButton
#property (nonatomic) NSIndexPath *indexPath;
#end
There should not be anything implemented in the .m file.
Create a UITableViewCell subclass which has the custom IndexPathButton on it. If you need help creating a custom UITableViewCell I suggest searching on SO or creating a new question.
In your UIViewController with your UITableView do the import of the custom UIButton
#import "IndexPathButton.h"
Also create an class variable to store the selectedIndexPath:
NSIndexPath *selectedButtonIndexPath;
Now we can start changing the UITableViewDataSource method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"SomeCell";
YourCustomCellWithButton *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell)
{
cell = [[YourCustomCellWithButton alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
//Default setup of IndexPathButton on YourCustomCellWithButton
cell.button.backgroundColor = [UIColor grayColor];
cell.button.indexPath = indexPath; //Here we store the indexPath for later use
[cell.button addTarget:self action:#selector(cellButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//Check here if it should be red or not
if (selectedButtonIndexPath == indexPath)
{
cell.button.backgroundColor = [UIColor redColor];
}
return cell;
}
And the button target action:
-(void)cellButtonPressed:(IndexPathButton*)sender
{
//Set the selectedIndexPath
selectedButtonIndexPath = sender.indexPath;
[yourTableView reloadData];
}
I Have a problem with my custom UItableViewCell. It's seems that all UIButton of UITableViewCell disappear on scroll. Specilay on my 25 cell. Any one have any idea ?
any help would be appreciated :)
That some code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* cellIdentifier = #"Cell";
Cell * __strong cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
InfoObject __weak *obj = [_pos objectAtIndex:indexPath.row];
[cell configCell];
cell.id = obj.id;
{// EVENT ON BUTTON
[cell.checkbox addTarget:self action:#selector(checkup:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnPaymentState addTarget:self action:#selector(changePaymentState:) forControlEvents:UIControlEventTouchUpInside];
}
{// SET BUTTON CHECKBOX
cell.checkbox.tag = indexPath.row;
[cell.checkbox setImage:[UIImage imageNamed:IMG_BLACK_UNCHECKBOX] forState:UIControlStateNormal];
[cell.checkbox setImage:[UIImage imageNamed:IMG_BLACK_CHECKBOX] forState:UIControlStateSelected];
[cell.checkbox setImage:[UIImage imageNamed:IMG_BLACK_CHECKBOX] forState:UIControlStateHighlighted];
}
{// SET BUTTON PAYMENT STATE
cell.btnPaymentState.tag = indexPath.row;
[cell.btnPaymentState setImage:[self getImageAssociateWithName:obj.stringPaymentState] forState:UIControlStateNormal];
[cell.btnPaymentState setImage:[self getImageAssociateWithName:obj.stringPaymentState] forState:UIControlStateSelected];
[cell.btnPaymentState setImage:[self getImageAssociateWithName:obj.stringPaymentState] forState:UIControlStateHighlighted];
}
[self SetSelectedBackgroundColorSelectedForFolderCellAtRow:indexPath.row
:obj.selected Cell: cell];
[cell.checkbox setSelected:obj.selected];
[self setCellColorTextOfObjectPrepare:cell withObj:obj];
return cell;
}
I solve my problem. The problem is that i was using Tag for my UIButton and the identifier was the same number tag of another UIView so when i was removing my UIView the UIButton disappear also. Thanks all for your answer.
Remove if (!cell)
And you're good to go.
i m having a simple section table view. I have added a button at the left side of image.This button is selectable.Only one button (each cell have single button of same type) can be selected at a time and selected button background image changes to a specific selected image, others button back ground image changes to deselect image.If you tap again on selected image it remains selected.
How ever i am not getting correct behaviour.
code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell=nil;
CustomCellMasterCustomer *cell=nil;
static NSString *cellIdentifier=#"Locations";
Customer *mCustomer;
cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
// cell=[self reusableContentWithIdentifier:cellIdentifier];
cell=(CustomCellMasterCustomer *)[self reusableContentWithIdentifier:cellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = indexPath;
[button setBackgroundImage:[UIImage imageNamed:#"cellLeftImageDisabled.png"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(10, 5, 36, 30)];
[button addTarget:self action:#selector(viewDetail:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
cell.aCustomer = mCustomer;//alok added
if (indexPath == self.mPreviousIndex)
{
[button setBackgroundImage:[UIImage imageNamed:#"cellLeftImageEnabled.png"] forState:UIControlStateNormal];
}
else
{
[button setBackgroundImage:[UIImage imageNamed:#"cellLeftImageDisabled.png"] forState:UIControlStateNormal];
}
return cell;
}
-(void)viewDetail:(id)sender
{
CGPoint buttonOrigin = [sender frame].origin;
// this converts the coordinate system of the origin from the button's superview to the table view's coordinate system.
CGPoint originInTableView = [self.allCustomerTable convertPoint:buttonOrigin fromView:[sender superview]];
// gets the row corresponding to the converted point
NSIndexPath *rowIndexPath = [self.allCustomerTable indexPathForRowAtPoint:originInTableView];
CustomCellMasterCustomer *cell = (CustomCellMasterCustomer *)[self.allCustomerTable cellForRowAtIndexPath:rowIndexPath];
[self fillDetailViewWithCustomerInformation:cell.aCustomer];
//change button background image
UIButton *btn = (UIButton *)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"cellLeftImageEnabled.png"] forState:UIControlStateNormal];
self.mPreviousIndex = rowIndexPath;
[self.allCustomerTable reloadData];
}
what wrong i am doing,
any suggestion
Try this. Because indexpath always recreate with same row and section number. It won't same as previous.
if (indexPath.row == self.mPreviousIndex.row && indexPath.section == self.mPreviousIndex.section)
{
[button setBackgroundImage:[UIImage imageNamed:#"cellLeftImageEnabled.png"] forState:UIControlStateNormal];
}
And this two line of code, not needed anymore
/* UIButton *btn = (UIButton *)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"cellLeftImageEnabled.png"] forState:UIControlStateNormal];*/
You should not add a button out side the if block. In UITableView cells will be reused, if the cell is reused and at already has a button added to contentView then you should not add one more on top of it. so create a custom cell and make the buttion instance visible outside the class by declaring the buttion property in customCell.h. Now check the condition -
if (selected)
{
cell.button.backgroundImage = selectedImage;
}
else
{
cell.button.backgroundImage = unselectedImage;
}
I have 1 UITableView has multiple UIButton (named is "Open"button with different tags) in each row, i set tag for them. Now, after click on any button in each row, it'll show a UIView (detailView). On detailView has 1 "Submit"button. I want to when user click on "Submit" button, "Open"button with tag selected is disable. How can i do that? I used this code :
Code to create tableview with mutiple "Open"button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
And code when click on"Open"button:
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateHighlighted];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
}
If you want your "Open" button to be disabled after you click submit button, you should use delegates
UIButton *btn = (UIButton *)sender;
btn.enabled = NO;
Try this one in button click method
EDITED :
Change you marketPressedAction parameter id to UIButton * and write code of below
-(void)marketPressedAction:(UIButton *)sender
{
[sender setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateSelected];
/// Here set you image ////////////////
[sender setImage:[UIImage imageNamed:#"myCustome.png"] forState:UIControlStateNormal];
[self createMarketPlaceForm]; // call detailView
sender.enabled = NO;
sender.userInteractionEnabled = NO;
}
When the user clicks on a UIButton on tableview ( tableview has mutiple button in each row), another View is shown.
I want to change image for this button after the user has clicked on the Done button of the other UIView. How can I do that? I'm a newbie. Could you please provide some code for me? Thanks in advance.
UPDATE CODE:
Code for tableview :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
[button setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
Code for Open button ( button that user click on to show another View)
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
}
And code for Done button:
-(void)submitMarket
{
for (UIButton *button in [_tableView subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == buttontag) {
[button setBackgroundImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
}
}
}
}
Try this :
[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];
Code on button click :
savedTag = button.tag;
Code on Done button click :
for (UIButton *button in [table subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == savedtag) {
[button setBackgroundImage:someImge forState:UIControlStateNormal];
}
}
}
In cellForRowAtIndexPath in place of this : [market setTag:3000]; write [market setTag:indexPath.row];
Try this :
Replace :
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
With this :
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag == indexPath.row;
}
}
One more thing : make first line of cellForRowAtIndexPath as :
NSString *CellIdentifier = #"tableCell";
Change your cellForRowAtIndexPath as :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
[market setTag:indexPath.row];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
else {
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
you can use a delegate or a NSNotificationCenter,
in your view where is Done button, you need create a property
#property (weak, nonatomic) <protocol_name> delegate;
and in your action method for DoneButton, you need to send a message to that delegate
[self.delegate method_name];
and the other view will be set as delegate for this one
`[viewWithDoneButton setDelegate:self];
and you need to implement the delegate method `
You can set another image for selected state of your button and after click on Done button just set selected state your button.
[yourButton setBackgroundImage:someImge forState:UIControlStateSelected];
after Done button:
[yourButton setSelected:YES];
Or if you use before UIControlStateSelected you can also use any other state. Or even users state - UIControlStateApplication