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];
}
Related
How can I keep a custom UIButton highlighted on selection, for about 5 seconds and then redirect to other view? In my code it is not showing any color when selected rather redirecting to other view.
This is my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect rect=CGRectMake(0+70*j,yy,79,40);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setContentMode:UIViewContentModeLeft];
button.titleLabel.font = [UIFont systemFontOfSize:14];
NSString *settitle=[NSString stringWithFormat:#"%#",item.TimeStart];
[button setTitle:settitle forState:UIControlStateNormal];
NSString *tagValue=[NSString stringWithFormat:#"%d%d",indexPath.section+1,i];
button.tag=[tagValue intValue];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setShowsTouchWhenHighlighted:YES];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
[hlcell.contentView addSubview:button];
[button release];
}
-(IBAction)buttonPressed:(id)sender
{
[sender setBackgroundColor:[UIColor cyanColor]];
Login *lgn=[[Login alloc] init];
[self presentModalViewController:lgn animated:NO];
[lgn release];
}
How can I do it ?
You have to set setShowsTouchWhenHighlighted:yes to the UIButton.
for example:
UIButton *click_btn=[UIButton buttonWithType:UIButtonTypeCustom];
click_btn.frame=CGRectMake(238,10, 70, 70);
[click_btn setTag:indexPath.row];
[click_btn setShowsTouchWhenHighlighted:YES];
[click_btn addTarget:self action:#selector(sample) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:click_btn];
you just add inside that method sleep(5);
for example:
This is user sample uibutton action method.
-(void)sample{
sleep(5);
sampleclass *classobject=[[sampleclass alloc]init];
[self.navigationController pushViewController:classobject animated:YES];
}
I wouldn't suggest using sleep() as this sleeps all threads and makes the app essentially dead for x seconds.
Highlight the button as per the instructions above and then use
[self performSelector:#selector(sample) withObject:self afterDelay:5.0f];
-(void)sample
{
UIViewController *myNewController = [[UIViewController alloc]init];
enter code here
[self presentModalViewController:myNewController animated:YES];
}
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.
I have created two custom button in UITableView.when i click on button1 the other button should be visible. for that i use following code. i am using UITableView grouped data type.
btnSettingButton=[[UIButton alloc]initWithFrame:CGRectMake(265, 50, 22, 22)];
[btnSettingButton setImage:[UIImage imageNamed:#"Settings Icon.png"] forState:UIControlStateNormal];
[btnSettingButton addTarget:self action:#selector(accessoryButtonTapped:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnSettingButton];
[btnSettingButton setHidden:YES];
btnUncheck=[[UIButton alloc] initWithFrame:CGRectMake(265, 15, 18, 18)];
//btnUncheck=[UIButton buttonWithType:UIButtonTypeCustom];
btnUncheck.tag=indexPath.row;
[btnUncheck setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
[btnUncheck addTarget:self action:#selector(buttonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnUncheck];
[btnUncheck release];
[cell.contentView addSubview:view];
return cell;
- (void)buttonTapped:(id)sender event:(id)event
{
CGPoint touchPosition = [[[event allTouches] anyObject] locationInView:self.tableV];
NSIndexPath *indexPath = [self.tableV indexPathForRowAtPoint:touchPosition];
if(favoriteChecked==NO)
{
[sender setImage:[UIImage imageNamed:#"YES.png"] forState:UIControlStateNormal];
if(indexPath){
[btnSettingButton setHidden:NO];
}
favoriteChecked=YES;
}
else
{
[sender setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
[btnSettingButton setHidden:YES];
favoriteChecked=NO;
}
}
when i use this code it shows the setting button to the last cell of the section.
please guide me how can i show settingButton to the cell which is clicked.
You only have one global variable for the btnSettingButton - of course it points to the last button you created.
You have to keep all references to all setting buttons you created, and then get the right one in your buttonTapped function. You could do it for example by adding all references to a dictionary, using the btnUncheck as a key - the btnUncheck is the sender you get in the button clicked function, so you can get the corresponding btnSettingButton from the dictionary.
Actually I am not clearly getting you.
I think you want to add a button in cell of tableview. If you click on this button , this image should be changed..
Am i right?
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
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];
}
}