Adding a delete button to UiTableViewCell - ios

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

Related

How to remove the button pragmatically with tag

I am adding button subView to each row I click. I am trying to remove the button subView every time once I click on different row and add button subview to the other row I click.
Where would be my issue? It is adding subView to each row without removing any of them.
if (sender.tag == 212)
{
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//Debug shows that every-time myButton&myButton2 hasn't tag??
if (myButton2.tag != 0 && myButton.tag != 0)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
UITableViewCell *cell = (id)sender.superview.superview.superview;
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:commonTable.table];
NSIndexPath *indexPath1 = [commonTable.table indexPathForRowAtPoint:rootViewPoint];
itemdata1 = [itemList objectAtIndex:indexPath1.row];
if (closeManager) [closeManager release];
CGFloat __y = commonTable.y + commonTable.table.y - commonTable.table.contentOffset.y + cell.height*indexPath1.row + sender.y;
CGFloat __x = commonTable.x + commonTable.table.x + sender.x;
CGRect layerBox1 = CGRectMake(618, __y, 90, sender.height+3);
CGRect layerBox2 = CGRectMake(888, __y, 94, sender.height+3);
NSLog(#"%f and %f", __x, __y);
myButton.frame = layerBox1;
[[myButton layer] setBorderWidth:2.0f];
myButton.layer.borderColor = [UIColor redColor].CGColor;
[myButton addTarget:self action:#selector(func1:) forControlEvents:UIControlEventTouchUpInside];
myButton2.frame = layerBox2;
[[myButton2 layer] setBorderWidth:2.0f];
[myButton2 addTarget:self action:#selector(func2:) forControlEvents:UIControlEventTouchUpInside];
myButton2.layer.borderColor = [UIColor redColor].CGColor;
[self addSubview:myButton];
[self addSubview:myButton2];
myButton.tag = myButton2.tag = 666;
}
Edited for Bhumeshwer katre:
if (myButton&&myButton2)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
//Other variables
if(!myButton){
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = layerBox1;
[[myButton layer] setBorderWidth:2.0f];
myButton.layer.borderColor = [UIColor redColor].CGColor;
[myButton addTarget:self action:#selector(func1:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 666;
[self addSubview:myButton];
}
don't create each time your button like this
Here you are creating each time new instance of UIButton
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
And you are doing this
//Debug shows that every-time myButton&myButton2 hasn't tag??
if (myButton2.tag != 0 && myButton.tag != 0)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
//It will just remove new instance of UIButton and no more your UIButton will be there on UIView.
try like this
if(!myButton){
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
if(!myButton2) {
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
//Don't remove your button just change frame
if you still want to remove buttons then first remove
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
//then create new instance of button
Try adding tags to buttons before adding them to subview.
So this:
[self addSubview:myButton];
[self addSubview:myButton2];
myButton.tag = myButton2.tag = 666;
should become like this:
myButton.tag = myButton2.tag = 666;
[self addSubview:myButton];
[self addSubview:myButton2];
Regards,
hris.to
If you use tableview cell, Use this following cases,
1) Set tag to button while creating tableViewCell.
2) Hide that button by cell.yourButton.hidden = YES
3) When you click on row, just note down that indexPath as self.selectedIndexPath = indexPath in didSelectRowAtIndexPath:
4) Now reload your tableView. Just add one condition in cellForRowAtIndexpath: as below.
if (indexPath.row == self.selectedIndexPath.row)
{
cell.yourButton.hidden = NO;
}
else
cell.yourButton.hidden = YES;
for (UIView *view in self.view.subView) { // instead of self.view you can use your main view
if ([view isKindOfClass:[UIButton class]] && view.tag == ?) {
UIButton *btn = (UIButton *)view;
[btn removeFromSuperview];
}
}
i think you have to set tag your buttons first,because default tag of every UIControl is 0;

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...

How to set the position of ImageView after getting x and y postion on finger touch in iphone

I am making an ipad app in which I have tableView with custom it works fine but I want that when user clicks on add button in custom cell then it should get the position x and y of that button and then set the imageView to the center of that button.
I have searced code it gets points but how to add make this for cell.addButton as cell is custom.
[cell.imageButton addTarget:self action:#selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
Here is the code for touch
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
But in this way it does not call the method which is on cell.addButton imageButtonAction.
As I understand from you , you need to set an image in a UIButton upon tap.
If you added the UITapGestureRecognizer you removed the default tap recognition and now only this would work:
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
You should remove the above code and only set this:
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
where the imageButtonAction: is :
- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
}
If you want to add an image next to the button let's say in the left of the button then the function should look like this:
- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];
}
UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];
//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:#"popupj.png"];
tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:#"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:#selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:#"popuptab1j.png"] forState:UIControlStateNormal];
[backimage addSubview:tab1];
now i am adding button to imageView but it does not get clicked
For UIButton you can use directly button function
-(IBAction) imageButtonAction:(id) sender
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
UIButton *btn = (UIButton *)sender;
//[btn setimage:[UIImage imagenamed:dimagename];
UIImageview *backimage = [[UIImageView alloc] initWithFrame:btn.frame];
[cell addSubview:backimage];
}

Button is supposed to change image after click, but (randomly) changes images of other buttons too

In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.

UITableView subview to uitableviewcell

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

Resources