UITableView subview to uitableviewcell - uitableview

i am trying to add a button to the tableview and no matter what frame i specify the button is not visible. This is a custom UITableViewCell and I am adding the button in the code rather than in the xib file is so that I can click on the button and perform an action which is not didSelectRowAtIndexPath:
I have the following code in cellForRowAtIndexPath: What am I missing?
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = bframe;
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;

try this
UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];

Related

How to remove UIButton in iOS7

I want to create and remove uibutton. (iOS7)
My viewDidLoad function creates uibutton in the following way:
for (char a = 'A'; a <= 'Z'; a++)
{
position = position+10;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(getByCharacter:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%c", a] forState:UIControlStateNormal];
button.frame = CGRectMake(position, self.view.frame.size.height-40, 10.0, 10.0);
button.tintColor = [UIColor whiteColor];
[self.view addSubview:button];
}
Now I want to remove specific uibutton based on button text.
- (IBAction)getByCharacter:(id)sender{
}
I don't know how to do this. Can anyone provide some sample code?
You can remove the button by calling
[sender removeFromSuperview];
in your callback (getByCharacter:). When you attach the selector to the button, the sender parameter will be the button which triggered the event. That means that you have an instance of it. With this instance you can now calll the removeFromSuperview method.
First you need to get the button's title text and then remove it from superView if it matches to the specific character,
- (IBAction)getByCharacter:(id)sender{
UIButton *myButton = (UIButton *) sender;
if([myButton.titleLabel.text isEqualToString:#"C"]{
[myButton removeFromSuperView];
}
}

Adding a delete button to UiTableViewCell

Since I am using a slide-out menu controller in my app - the swipe to delete on a UITablewViewCell no longer works as the pan gesture is used to open / close the side menu.
So, I was thinking about adding a delete button to show up on each cell all the time - so user can just tap delete to remove the cell.
I added this code to UItableView cellforRowAtIndexPath method:
/* Remove Button */
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:#"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
action:#selector(removeItem:)
forControlEvents:UIControlEventTouchUpInside];
This adds the button and in the remove method I am not sure how to go about actually removing the correct cell.
Can anyone point me in the right direction here?
Thanks!
Basically you need index for the cell which has to be deleted when delete is pressed.
you can set the tag property and when button is pressed you can check the tag propery of the button which is sending event.
see below code,
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.tag = indexPath.row;
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:#"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
action:#selector(removeItem:)
forControlEvents:UIControlEventTouchUpInside];
-(void) removeItem:(id) sender
{
UIButton *button = (UIButton*)sender;
int index = button.tag;
}
Try this,
- (IBAction)removeItem:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
//Remove the cell at indexPath
}
//1. add tag to the button, for identifing later
removeButton.tag = indexPath.row;
//2. Get the cell
UITableViewCell *cellToBeDeleted = [tableView cellForRowAtIndexPath:sender.tag];

how to add the UITableView detailDisclosurebutton custom in ipad app

I have tableView with detailDisclosure button it works fine but i want to give title to detail disclosure button so i think i need to add custom UIButton for detailDisclosure is it possible to do like i want to do this without using custom cell.
If i want to give image it works fine but how may i give title to this button like comment.
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"loginN.png" ]];
Try this..
UIButton *btn = [[[UIButton alloc]init]autoerelease];
btn .frame = CGRectMake(210.0, 0.0, 90, 25);
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"loginN.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubView: btn];
and in btnClickHandler do this to get indexPath
- (void)btnClickHandler :(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Get your data source object from indexPath.row.
}
Why don't u set the a button as accessory view just like u try setting an image..??
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 80.0, 80.0);//Set the frame u need for the view
//Customize the button
[button setTitle:#"title" forState:UIControlStateNormal];
//And set it as accessory view
cell.accessoryView = button;
Here is the result...

trying to add a uibarbuttonstyleCamera to a uitableviewcell in ios6

Im trying to add a uibarbutton- Camera style to a uitableviewcell programmatically like so
but I dont see anything in the uitableviewcell
UIButton *newBtn=[UIButton buttonWithType:UIBarButtonSystemItemCamera];
[newBtn setFrame:CGRectMake(250,10,50,30)];
[newBtn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
but when I use this code, I see a rounded rect button in the uitableviewcell
UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(250,10,50,30)];
[newBtn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
Is there any way I can add a uibarbuttonstyle button or all I can do is add a uibutton rounded rect style to the uitableviewcell?
UIBarButtonItem is not the same as UIButton. It doesn't even inherit from UIView, so you can't add it as a subview to other views. What happens when you call
[UIButton buttonWithType:UIBarButtonSystemItemCamera]
is that the UIBarButtonSystemItemCamera constant is of an invalid value for buttonWithType:, so UIButton can't decide which type of button to return - probably it just returns an empty, not configured one or nil.
So there's no easy solution to your problem. What you can do is, for example, take a screenshot of the Camera bar button item, then extract its image using some kind of image editor application, then set that image as the image of a custom UIButton like this:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"Camera.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:btn];

Why I can't see my UIBUTTON on my iPad but I see my UIBUTTON on the simulator?

Introduction:
I'm creating my UIBUTTON at runtime. And my UIButton is in a TableView. The whole function of this UIButton is: If I click on "delete OFF" or "delete ON" (normal void Method) I can delete each cell in my TableView. I took the position of my UIButton on the right side like the real delete-method.
First of all, I know that Objective-C has got a delete-method! But I cant use this method, because I have Sliders in my TableView and sliders with Objective-C delete method doesn't work together.
Because of this, I have written this Code:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
if (allowchange==YES) //<- Button to activate the delete mode
{
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(700, 23, 45, 25);
[playButton setBackgroundImage:[UIImage imageNamed:#"button_delete.png"]
forState:UIControlStateNormal];
[self.view addSubview:playButton];
[playButton addTarget:self action:#selector(deleteRow:) forControlEvents:UIControlEventTouchDown];
playButton.tag=count;
[cell.contentView addSubview:playButton];
[playButton release];}}

Resources