swipe to delete action replaced by button [duplicate] - iphone

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Replace action of swipe to delete by clicking on a button
I have the following code.
- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[appointments removeObjectAtIndex: indexPath.row]; // manipulate your data structure.
[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath]
withRowAnimation: UITableViewRowAnimationFade];
NSLog(#"row deleted"); // Do whatever other UI updating you need to do.
}
}
This piece of code is going to be execute when I swipe on the cell. But what I want is that this code executes when I press a button in my custom tableview cell. Like you can see on the screenshot below I have 2 buttons on my tableview.
When the user presses the 'X' button the delete button should roll out like when you swipe the cell. I have the following action attached to my cell.
-(IBAction) deleteAppointment:(id) sender{
//show swipe delete button
}
And attached it in my cellForRowAtIndex at the following way.
cell.deleteAppointment.tag = indexPath.row;
[cell.deleteAppointment addTarget:self action:#selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];

just addTarget with button in your cell and set tag to every button like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/////your code
UIButton *btnClose = [[UIButton alloc]initWithFrame:CGRectMake(270, 7, 20, 20)];
btnClose.tag = indexPath.row;
[btnClose setImage:[UIImage imageNamed:#"closeImage.png"] forState:UIControlStateNormal];
[btnClose addTarget:self action:#selector(deleteAppointment:) forControlEvents:UIControlStateNormal];
[cell addSubview:btnClose];
return cell;
}
and in the method see like this...
- (IBAction)deleteAppointment:(id)sender {
// here bellow code for swipe the close button in cell
UIButton *btn = (UIButton *)sender;
int SelectedRowNo = btn.tag;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[btn setFrame:CGRectMake(btn.frame.origin.x - 50, btn.frame.origin.y, btn.frame.size.width, btn.frame.size.height)];
[UIView commitAnimations];
//// here bellow code for delete raw from table
/*
[appointments removeObjectAtIndex: SelectedRowNo];
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [yourTableView indexPathForCell:clickedCell];
[yourTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: clickedButtonPath]
withRowAnimation: UITableViewRowAnimationFade];
NSLog(#"row deleted"); // Do whatever other UI updating you need to do.
*/
}
i hope this help you..

Related

I have a drawer of buttons in UITableViewCell. How do I get the indexPath of the cell and delete it?

I have implemented this button drawer and I've added some buttons to it. That said, I'm unsure how I'll go about sending messages from these buttons in the drawer to the appropriate delegate method to delete the item from my tableView.
How do I get the correct indexPath? Should I make a new NSIndex for uitableviewcells that have had their check button toggled?
Thanks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSInteger directionMask = indexPath.row % 5;
if (cell == nil) {
cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];
drawerView.backgroundColor = [UIColor grayColor];
cell.drawerView = drawerView;
UIImage *checkImage = [UIImage imageNamed:#"check.png"];
UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[checkButton setImage:checkImage forState:UIControlStateNormal];
cell.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
checkButton.frame = CGRectMake(10, 10, checkImage.size.width, checkImage.size.height);
[drawerView addSubview:checkButton];
[checkButton addTarget:nil action:#selector(onCheckMarkTap:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)onCheckMarkTap {
NSLog(#"Delete the cell");
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
This question already has an answer. You get the coordinates of the button that was tapped in the table view's coordinate system using target/action with an event or via a gesture recognizer, then use UITableView's method -indexPathForRowAtPoint: to get the indexPath of the cell containing the button at that point.
Within cellForRowAtIndexPath:
checkButton.tag = indexPath.row
Also inside onCheckMarkTap, do this:
- (void)onCheckMarkTap : (id) sender
{
if (sender.tag == 0)
///code
else if (sender.tag == 1)
///code
}
The way I handle problems like this in my own code is to subclass "UIButton", where I add in a "NSIndexPath" or "tableRow" ivar into the subclassed button. Let's name is "StangButton".
With a subclassed button, you can populate the "indexPath" ivar when you are composing the cell via "cellForRowAtIndexPath:".
Then, when the button is pressed, you'll know which button was pressed and which row it was in.
You can also check to see if the "tableView:didSelectRowAtIndexPath:" table view delegate method is called when the button is touched in the cell.

passing the toggle button value to switch button state in another class in iphone

i have an application in which i have 2 tableview controller.In first tableview, i have a button created in my cellforRowAtIndexPath of my first tableview. Initially i have set the button image to 'ON' image which means my alarm is on .Then when i click on the edit button my tableview changes to editmode where i can change my button image to 'OFF'.i want that my button action should be called when the tableview enters in edit mode.This is the code that i have written.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d", indexPath.section, indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
TAlarmCell *cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = 1;
[mimageButton setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
[mimageButton setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:mimageButton];
}
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//this is my edit code.
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[mTableView setEditing:editing animated:YES];
if (editing)
{
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = nil;
}
else {
self.navigationItem.rightBarButtonItem = addButton;
[self.mTableView reloadData];
}
}
//this is my button action
-(void)changeMapType:(UIButton *)sender
{
NSIndexPath *indexPath =[self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
NSLog(#"row...%d",row);
appDelegate.changeimagetype = !appDelegate.changeimagetype;
sender.selected = appDelegate.changeimagetype;
[self.tableView reloadData];
}
But the problem here is when i click on edit button and tableview enters in edit mode,changeMapType: action is not getting called and my image does not changes.
can u give me some details about
where u r setting this flag
if (editing)
and for which action of user u want this method to get to called..?
-(void)changeMapType:(UIButton *)sender
if u want to get called immediately after entering into edit mode, u can use the below method
[self performSelector:#selector(changeMapType:)];

How to generate a UITextField and UIButton in UIScrollView and remove these items dynamically

HI stackoverflow friends
I have situation that when i click on a button it will generate dynamically a uitextfield with a remove button. If I don't need that text field then i click the remove button it will goes off.More over the user can generate infinite no: of these pair. So that I think it should be add in a scrollview. Is it possible this. Can anyone know how to do this?
Any help would be appreciable.
In your TableView cellForRowAtIndexpath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField* textField;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10,20,30,30)];
textField.tag = 10;
[[cell contentView] addSubView:textField];
[textField release];
}
else
{
textField = (UITextField *)[cell.contentView viewWithTag:10];
}
// Do something with TextField
return cell;
}
You can just have one add button on the navigation bar across top right that would add to NSMutableArray addObject textField to it and reloadTable. Inside cellForRowAtIndexPath get objectAtIndex for that array and add to cell contentView. To delete the cells you can implement swipe to delete for cells...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If row is deleted, remove it from the Array.
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// delete your data item here
}
}
Posted some code that should help you:
// alloc + init + set appropriate size and origin
UIScrollView *scroll;
UITextField *field;
UIButton *button;
// add subview
[scroll addSubview:field];
[scroll addSubview:button];
// remove subview
[field removeFromSuperview];
[button removeFromSuperview];
// memory managment
[field release];
[button release];

adjusting UITableView's cell at runtime

I have subclassed a UITableViewCell. What I want to have is when I click on the cell, I added a UIView to the bottom of the cell and adjust the height of the cell accordingly. Here's my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell* cell = (MyCell *) [self.table cellForRowAtIndexPath:indexPath];
UIView * options = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 27, cell.frame.size.width, 27)];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button2];
UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button3];
//did some layout calculation here to position the button
[cell addSubview:options];
}
Here's a video that illustrates the issue
Also I've tried to change the accessoryView of my cell from my custom UIButton to the regular UITableViewCellAccessoryDisclosure but it didn't work
MyCell * cell = (MyCell *)[self.table cellForRowAtIndexPath:temp];
[cell.accessoryView removeFromSuperview];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
If you're curious what I am trying to do, I am trying to create a tweetbot uitableviewcell, where you press the cell/row and it presents you with other options (retweets, etc) and also it was shown animated.
I don't know if I correctly understand what you are trying to do. But if you do want to animate the change of the UITableViewCell's height try to adjust it within - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and call
[tableView beginUpdates];
[tableView endUpdates];
Put controls into another cell and use
insertRowsAtIndexPaths:withRowAnimation:
to show it and
deleteRowsAtIndexPaths:withRowAnimation:
to hide it.

UIButton Event for Delete Row in UITableView

I add button in UITableView cell. My code is
UIButton *btnView = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain];
btnView.frame = CGRectMake(280.0, 8.0, 25.0, 25.0);
//[btnView setImage:[UIImage imageNamed:#"invite.png"] forState:UIControlStateNormal];
[btnView addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnView];
[btnView release];
My button action is
- (void)action:(id)sender
{
}
I want to know, how to get current table row index when click. I want to delete table row when click on button.
Edit::
I found a way for delete with animation
[listOfItems removeObjectAtIndex:indexPath.row];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
I believe one approach is to store the row number in the button tag
btnView.tag = [indexPath row]
in the action
- (void)action:(id)sender
{
UIButton * btn = (UIButton)sender;
int row = btn.tag;
}
I usually use this method:
- (IBAction)buttonAction:(id)sender {
UIButton *button = sender;
UIView *contentView = [button superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Do something
}
Depending on the layout of your UITableViewCell it is possible that you have to traverse deeper into your TableViewCell Views.
http://nshipster.com/associated-objects/ Use an associated object of a NSNumber surrounding the row or the index path itself as the associated object
//when you create the button
objc_setAssociatedObject(button, associationKeyForButton, [NSNumber numberWithInteger:indexPath.row], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//in the touch up inside handler to get the row back.
NSNumber* rowNumber = objc_getAssociatedObject(sender, associationKeyForButton);