how to change tableView Cell button on click in iphone - iphone

I have table view with custom cell and it has button i want that when user click on that button then the image of button should be changes any idea how to do this becuase i want that cellImage button image to be changed in another method.
[cell.theSyncButton addTarget:self action:#selector(syncButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.theSyncButton setTag:indexPath.row];
method:
-(IBAction)syncButtonAction:(id)sender{
// I want to change the cell.SyncButton image here
}
any idea how to do this.

[btnSync setImage:[UIImage imageNamed:#"refresh.png"] forState:UIControlStateNormal];
[btnSync setImage:[UIImage imageNamed:#"refresh-active.png"] forState:UIControlStateHighlighted];

-(IBAction)syncButtonAction:(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:img forState:UIControlStateSelected];
}

Instead of all that code you have all you have to do is:
UIImage *img = [UIImage imageNamed:#"yourImage.png"];
[cell.theSyncButton setImage:img forState:UIControlStateSelected];

try this..
-(IBAction)syncButtonAction:(id)sender
{
UIButton * syncButton = (UIButton *) sender;
[syncButton setBackgroundImage: <NormalImage.png>
forState: UIControlStateNormal];
[syncButton setBackgroundImage: <HighlightedImage.png
forState: UIControlStateHighlighted];
}

Here is my solution:
i use UIImageVIew as button
for example , the starIcon is an UIImageView ,which i add into my custom UITableViewCell as button.
and ArticleCell is my custom UITableViewCell class.
in UITableView dataSource method cellForRowAtIndexPath:
ArticleCell* cell = [tableView dequeueReusableCellWithIdentifier:#"reused"];
if (cell==nil) {
NSLog(#"init tableViewCell");
cell = [[ArticleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"reused"];
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(buttonCallback:)];
gesture.numberOfTapsRequired=1;
gesture.numberOfTouchesRequired=1;
[cell.starIcon addGestureRecognizer:gesture];
cell.starIcon.userInteractionEnabled=YES;
NSLog(#"init tableViewCell end");
}
make sure you set userInteractionEnabled == true;
then , in your buttonCallback method ,you can get UIimageView by
-(void)buttonCallback:(UIGestureRecognizer*)gestureRecognizer{
UIImageView* star = (UIImageView*)[gestureRecognizer view];
}
so , you can change your imageView.

-(IBAction)syncButtonAction:(id)sender{
[button setImage:[UIImage imageNamed:#"yourimage.png"] forState:UIControlStateNormal];
}

Related

Overwriting an image button with another image when I click that button

I have created an image button (a.png) infront of each row in the cellForRow method. In the same method itself I'm calling a buttonPressed method which has the code to be executed when the button is pressed. And I want that on the pressing of the button, the button image should be changed to another image (b.png) of the same size.
Please help with how to do that.
[but setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
try this......
Create Custom Cell for UITableView and UIButton on Custom Cell.
Create property for UIButtion.
In cellForRowAtIndexPath: method
//Set Action on Button in Table View Row
[cell.UIButtonOutlet addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
This will create action from your button.
And in method
-(void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches=[event allTouches];
UITouch *touch=[touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.historyTableView];
//GET IndexPath
NSIndexPath *indexPath=[self.TableView indexPathForRowAtPoint: currentTouchPosition];
selectedCell= [PairTableView cellForRowAtIndexPath:indexPath];
[selectedCell.UIButtonOutlet setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Try the following method
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button setFrame:CGRectMake(12,12,200,200)];
[button addTarget:self action:#selector(click: withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
then in the click action method of button
-(void)click:(UIButton *)button :withEvent:(UIEvent *)events{
[self yourFunction:(UIButton *)button];
}
then finally
-(void)yourFunction:(UIButton *)button{
button.selected = ! button.selected;
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
}
}
Try this when initializing your button
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Then modify your method(which is called when button is pressed) like this
-(void)yourMethod:(id)sender{
UIButton *btn=(UIButton*)sender;
[btn setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
//your other code
}

Add a Button to a TableView Cell:

I have a UI tableView and I am trying to create a button like the one used in the Safari Settings to Clear History:
The code I have tried doesnt really work and I'm sure there is a better way:
case(2):
// cell = [[[ButtonLikeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:cell.bounds];
[button addTarget:self action:#selector(clearButtonClick) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor purpleColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:#"Clear Data" forState:UIControlStateNormal];
cell.accessoryView = nil;
cell.clipsToBounds=YES;
[cell.contentView addSubview:button];
}
break;
}
}
return(cell);
}
A subclass on the tableview cell is overkill for this, basically all you need to do is set the text alignment of the textLabel to center
case(2):
if (indexPath.row == 0)
{
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
break;
no need to use an actual UIButton.
the only difference between does rows and "normal" rows is the position of the label.
subclass UITableViewCell and override -laysubviews to center the label and you're done, like so:
#interface ButtonLikeCell : UITableViewCell
#end
#implementation ButtonLikeCell
-(void)layoutSubviews
{
self.titleLabel.center = self.center;
}
#end
this will reposition the label of the cell and make it look similar to what you're looking for.

how to put custom image in table view cell

In my app i have to implement checkbox functionality in tableview cell.By clicking on that checkbox another label will be created in the same cell.1st how to implement checkbox functionality?i have created custom cell. here is my code which i tried but it doesnt work.
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UIButton *btnUncheck=[[UIButton alloc] initWithFrame:CGRectMake(260, 35, 20, 20)];
btnUncheck=[UIButton buttonWithType:UIButtonTypeCustom];
btnUncheck.tag=indexPath.row;
// [btnUncheck setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
[btnUncheck addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnUncheck]
-(void)checkBoxClicked:(id)sender{
if(favoriteChecked==NO)
{
[sender setImage:[UIImage imageNamed:#"YES.png"] forState:UIControlStateNormal];
favoriteChecked=YES;
}
else
{
[sender setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
favoriteChecked=NO;
}
}
Cast your sender from id type to button type using UIButton *btn = (UIButton *)sender; and then change image for btn.
Also change [view addSubview:btnUncheck] to [cell.contentView addSubview:btnUncheck]; And for doing that you will require to create a TableViewcell
Hope this helps.
try this
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
button.frame = CGRectMake(3,8,30, 30);
[button setImage:[UIImage imageNamed:#"chack box1_selected_callback.png"] forState:0]
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
}
in the block
if(cell==nil)
{
//add the code here
}
Hope this helps

Way to change icon of accessory type in UITableviewcell

I have table view which is showing list of files to download. When I tap the accessory button it will download the selected file. I want to change image of detail disclosure button. Is it possible....?
And If I use Button with image in accessory view. Is there any table delegate method for this...
Answer2: You can make your own method and call that in that case.
int row=indexPath.row;
UIButton *trackImageOnMap=[[UIButton alloc] initWithFrame:CGRectMake(420, 9, 40, 50)];
[trackImageOnMap setImage:[UIImage imageNamed:#"track_map_icon.png"] forState:UIControlStateNormal];
int iId=[[self.imageId objectAtIndex:row] intValue];
NSLog(#"iId=%d",iId);
[trackImageOnMap setTag:iId];
[trackImageOnMap addTarget:self action:#selector(trackImageOnMapButtonTouched:)forControlEvents:UIControlEventTouchDown];
[trackImageOnMap setContentMode:UIViewContentModeScaleToFill];
//[cell setAccessoryView:trackImageOnMap];
[cell addSubview:trackImageOnMap];
You could create a UIImageView and assign it to the UITableViewCell's accessoryView.
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"accessoryIcon"]] autorelease];
cell.accessoryView = imageView;
If you want a button for the accessoryView it's basically the same:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"buttonImage"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = cell.indexPath;
cell.accessoryView = button;
If you want to replace a default type, you can overwrite UITableViewCell and use the following code:
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType
{
if (accessoryType == UITableViewCellAccessoryDisclosureIndicator)
{
self.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YourNewImage.png"]] autorelease];
}
else
{
[super setAccessoryType:accessoryType];
}
}

UIButton to cover a UITableViewCell

I'd like one of my table rows to be a button that takes up an entire row of my UITableView. I figured the best way to go about this is to instantiate a UIButton, and give it the same frame size as an instance of UITableViewCell, and add that as a subview to the cell. I'm almost there, but quite a few pixels off to not get that perfect touch to it. Is there a better approach to this, or perhapsps can my placement accuracy be fixed up to get that perfect alignment?
cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self tableviewCellWithReuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 5.0f, 320.0f, 44.0f)];
[button setTitle:#"Do Stuff" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
You can fake it in your didSelectRowAtIndexPath: method.
Have the tableView cell act as the button.
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
[self doStuff];
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
Use setSelected: animated: to fake a button press and have it fade.
Might do the trick for you.
Class(UITableViewCell) has contentView property.
So I put basic button in contentView of UITableViewCell variable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
・・・
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
[btn setTitle:#"OK" forState:UIControlStateNormal];
[btn setTitle:#"OK" forState:UIControlStateSelected];
[btn addTarget:self action:#selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
・・・
return cell;
}
Sorry, poor my English.
if you want it to fit perfectly, just call the cell's bounds as your CGRect. do it like this:
button.frame = cell.bounds;
That should get your button to be exactly the same as your cell. Hope that helps!
Cord
This may be beside the point, but your code may have the problem where another button is added to the cell every time the cell is re-used.
I had the same problem as you. And Ryan gave you the answer: use the cell itself as a button with the didSelectedRowAtIndexPath: method.
You can also add add a UIImage to your UITableViewCell to make the cell appear to be a button.
You can also create a view with a button and place it in the footer of the section above it. That way you don't need to worry about the cell image in the background. I've done this to place two half-width buttons side-by-side in a grouped table view.
FWIW, I put the button in my app at CGRectMake(9, 0, 302, 45)
and I think it looks perfect.
UIButton *btnView = [[UIButton alloc] init];
btnView.frame = CGRectMake(52.0f, 2.0f, 215.0f, 38.0f);
[btnView setImage:[UIImage imageNamed:#"invite.png"] forState:UIControlStateNormal];
[btnView addTarget:self action:#selector(showInviteByMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnView];
[btnView release];