Add a Button to a TableView Cell: - iphone

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.

Related

how to change tableView Cell button on click in 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];
}

Adding button into UITableViewCell

I am trying to add button into my table cell like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UIButton* buttonCheckbox = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCheckbox.frame = CGRectMake(0, 177, 56, 26);
buttonCheckbox.backgroundColor = [UIColor redColor];
[buttonCheckbox addTarget:self action:#selector(actionFirst1) forControlEvents:UIControlEventTouchUpInside];
[buttonCheckbox setTitle:#"MyTitle" forState:UIControlStateNormal];
[cell addSubview:buttonCheckbox];
but actionFirst1 event is not fired. if i add the button [self.view addSubview:buttonCheckbox] instead of [cell addSubview:buttonCheckbox] it works fine. why?
thanks...
You should add this button to the contentView property of your cell
[cell.contentView addSubview:buttonCheckbox];
In fact, all your custom elements inside a UITableViewCell should be placed inside the contentView as this cell elements (contentView, accessorView and image) are all placed on top of any elements that your cell have.
Check this doc for more info on cell about custom UITableviewCell
Hi try below code it may help you out:
if ([indexPath row] == 0)
{
UIButton *myButton = [[UISwitch alloc]initWithFrame:CGRectMake(x, y, width,
heigth)];
[cell addSubview:myButton];
cell.accessoryView = myButton;
[myButton addTarget:self action:#selector (actionFirst1)
forControlEvents:UIControlEventTouchUpInside];
}
This code will help you out. Please set frames according to your cell of TableView.
Please see whether it would suffice to add your button as the accessoryView of the table cell. In that case, the iOS framework will position the button in the right place for your cell style itself (though you still need to give it a size). Here's an example from one of my projects:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 100, 27); // only size matters
[myButton setTitle:#"Connect" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(connect:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = myButton;
That's it. You just create the control, make sure it has a size, and set it to the accessoryView.

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];