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
Related
I had an application in which I am adding some dynamic number of buttons to a scrollview. I had set a normal background and selected background for the UIButton. For some reasons I need to call the UIButton sender method programmatically by:
[self buttontapped:nil];
as this but it is not changing the background of the button by using the code:
button.selected = YES;
I had set the background like this initially of the button:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
int j = i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected = NO;
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[tabBarS addSubview:btn];
-(void)buttonTapped:(id)sender {
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
btn.selected=NO;
}
btn = (UIButton *)sender;
NSLog(#"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
btn.selected = YES;
}
But everything except change of background only working. Where am I going wrong?
i done with bellow method call your UIButton method like:-
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected=NO;
[btn addTarget:self action:#selector(yourButtonAction:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
btn.tag = i;
[tabBarS addSubview:btn];
and it's Action Method should like with sender
-(IBAction)yourButtonAction:(id)sender
{
UIButton *btn = (UIButton*)sender;
if (![btn isSelected])
{
[btn setImage:[UIImage imageNamed:#"selected_fb_btn.png"] forState:UIControlStateNormal];
[btn setSelected:YES];
[self login];
}
else{
[btn setImage:[UIImage imageNamed:#"facebook.png"] forState:UIControlStateNormal];
[btn setSelected:NO];
}
}
EDIT
You can call button action event programeticuly using bellow trik:-
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
write this bellow line first and also change the method type from void to IBAction...
btn = (UIButton *)sender;
like bellow...
-(IBAction)buttonTapped:(id)sender {
btn = (UIButton *)sender;
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
[btn setSelected:NO];
}
NSLog(#"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
[btn setSelected:YES];
}
You need to set image property rather than backgroundImage property of UIButton.
Use default Image property and selected property.
[btn setSelected:TRUE];
is the log that you are printing, prints the button.tag correctly?
NSLog(#"Tab bar %d is clicked",btn.tag);
In your button action
btn.selected=YES;
in last line makes the button in selected state always
also get the correct instance as
btn=(UIButton *)sender;
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
}
I have dynamically created some list of RadioButtons with some values. pro grammatically I changed the button state and changed the image for selected and unselected. but the problem is i can select the all radioButtons at same time. actually I need to select one at a time.
when I clicked the next RadioButton, previously selected button state should be changed to non-selected.
Here is my code, I tried with changing image, but ...some problem with my code.
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender ;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
where can I change the image of previously selected button.
thanks in advance
Deselect all the buttons when you select one. If you have buttons in your scroll view you can user this code:
//Your Method
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender;
[self deselectAll];
[self radiobuttonAction:RadioButton1];
}
- (void) deselectAll : (UIScrollView *) scrollView{
NSArray *viewArray = [scrollView subviews];
for (UIView *v in viewArray){
if([v isKindOfClass:[UIButton class]]){
[((UIButton *)v) setSelected:NO];
}
}
}
EDIT: But if you want to give it real radio button effect (in which one is always selected, and only one is selected) it will be more easy to you. Use following code:
//A globle refButton
UIButton *refButton = nil;
//Set image for both state:
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateSelected];
//make any of above default select: may be the last one and pass that to `refButton`
refButton = RadioButton;
//Your Method
-(void)RadioButtonTapped:(id)sender
{
[refButton setSelected:NO];
refButton = (UIButton*)sender;
[refButton setSelected:YES];
}
Tag the button at the time of creation.
When button tapped, get the buttons through tag. Deselect all buttons. Update current button which you have received as a n argument.
assuming you want to add 5 radio buttons.
- (void)viewDidLoad
{
for (int i = 0; i < 5; i++) {
UIButton* RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0, i * 40)];
// [RadioButton setTag:i * 10];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
}
//do not forget to declared this in your header file
-(void)RadioButtonTapped:(UIButton*)button;
{
for (UIButton *btn in self.scrollView.subviews) {
[btn setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
}
[button setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
the code above updates the UI, sets the image of all buttons to "uncheck" then changes the image of the last pressed button to "check".
create buttons like this,
for(int i=0;i<4;i++)
{
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
RadioButton.tag = i*100;
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
add action like this,
-(void)radiobuttonAction:(UIButton *)Button
{
UIButton *Button = (UIButton*)sender;
for(UIButton * btn in self.scrollview.subViews)
{
if(btn.tag == Button.tag)
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
else
{
//do selected or de-selected code for other buttons
}
}
I want to create custom button in table view.in that by clicking on that button the label will be create with current date.In that button i also have to implement checkbox functionality. I have implemented following code for checkbox functionality:It works fine but i can i create label by checking them YES.Thanks in advance.
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];
[btnUncheck release];
[cell.contentView addSubview:view];
return cell;
}
-(IBAction)checkBoxClicked:(id)sender
{
if(favoriteChecked==NO)
{
[sender setImage:[UIImage imageNamed:#"YES.png"] forState:UIControlStateNormal];
favoriteChecked=YES;
[lblAchievementreward setHidden:NO];
}
else
{
[sender setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
favoriteChecked=NO;
}
Yes, you can. In checkBoxClicked:, save the row of the checked item in an instance variable. Then, reload the row so that cellForRowAtIndexPath is called again. Something like-
self.checkedRow = sender.tag;
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathWithIndex:sender.tag]] withRowAnimation:UITableViewRowAnimationNone];
Then, in cellForRowAtIndexPath:, check for self.checkedRow and add a label. Something like-
if(indexPath.row == self.checkedRow)
{
UILabel* label = [[UILabel alloc] init];
//label.text = [NSDate date]; //use date formatters here
[cell.contentView addSubview:label];
}
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];
}
}