How to handle user click the "delete button " in setEditing in TableView? - iphone

I added a editButton on the table like this:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
and, having a setEditing method:
- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.watchListDetailTableView setEditing:editing animated:animated];
if (editing) {
// you might disable other widgets here... (optional)
} else {
// re-enable disabled widgets (optional)
}
}
after I click the edit, I can have a cross and delete button, which method should I do to handle the delete button click? thank you.

This should be it:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
More Info Here
When users tap the insertion (green
plus) control or Delete button
associated with a UITableViewCell
object in the table view, the table
view sends this message to the data
source, asking it to commit the
change. (If the user taps the deletion
(red minus) control, the table view
then displays the Delete button to get
confirmation.) The data source commits
the insertion or deletion by invoking
the UITableView methods
insertRowsAtIndexPaths:withRowAnimation:
or
deleteRowsAtIndexPaths:withRowAnimation:,
as appropriate.

Related

How to create dropdownlist without using UIPIckerviewcontroller in IPhone?

I want to dropdownlist like listview at when i click the dropdown button and make the list have some contents. then any content i select it will be text for the label any one help me. Thanks Lot.
You can use pop-over to display list.In pop-over you can create tableview to display list of items and when user selects any option, didSelectRowAtIndexPath will be called, from this method you can send the selected value and display in label.
Code in mainviewcontroller, where you want to display drop down.
if (m_OptionController !=nil)
{
[m_OptionController release]; m_OptionController = nil;
}
m_OptionController=[[OptionViewController alloc]init];
[m_OptionController setTarget:self andSelector:#selector(displaySelectedOption:)];
if(m_pPopOverController)
{
[m_pPopOverController dismissPopoverAnimated:YES];
[m_pPopOverController release];
m_pPopOverController=nil;
}
m_pPopOverController=[[UIPopoverController alloc]initWithContentViewController:m_OptionController];
[m_pPopOverController setPopoverContentSize:CGSizeMake(thePopOverFrame.size.width, thePopOverFrame.size.height) animated:NO];
[m_pPopOverController presentPopoverFromRect:CGRectMake(theButton.frame.origin.x,0,40,40) inView:self
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
OptionViewController is a UIViewController which will contain UITableView.Populate UITableView with data(list of options).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([m_Target respondsToSelector:m_Selector]) {
[m_Target performSelector:m_Selector withObject:nil];
}
}
Do not forget to set target by calling this method, so when user selects any option, corresponding method in mainviewcontroller is called where you want selected value.
- (void)setTarget:(id)inTarget andSelector:(SEL)inSelector
{
m_Target = inTarget;
m_Selector = inSelector;
}

Enable all textfields in UITableViewCell when in Edit mode

I have a bunch of custom UITableViewCells with a label and textbox. I have the textbox disabled but I want to make it so when the user taps the Edit button it will make the textboxes editable. How can I do this so that ALL the UITextFields in the UITableView become enabled?
I have
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.navigationItem setHidesBackButton:editing animated:YES];
if (editing) {
}
}
but cannot add the textbox enable in there since I don't have access to all the textfields. Would I need to add code to grab all the cells and loop through them and enable the textfields?
I would do this by setting a isEditing BOOL on your UITableViewDelegate in the setEditing:animated: method and just updating visible cells when the value is changed.
NSArray *visibleCells = [myTable visibleCells];
for (MyTableViewCell *cell in visibleCells)
cell.textField.enabled = isEditing;
Then, using your UITableViewDelegate again, update new cells as they appear in tableView:willDisplayCell:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textField.enabled = isEditing;
}
Edit your subclass of UITableViewCell and register your instances for an editing notification in your subclass's viewDidLoad or init method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(disableTextBox) name:#"EditingIsEnabled" object:nil];
And implement a method called disableTextBox that disables the text box for that cell.
Then in your setEditing:animated method, post the notification when you want to start editing:
[[NSNotificationCenter defaultCenter] postNotificationName:#"EditingIsEnabled" object:self];
Override the method dealloc in your UITableViewCell and remove yourself as an observer, or you'll crash:
[[NSNotificationCenter defaultCenter] removeObserver:self];
If you're not using ARC, make sure to call [super dealloc]. If you're using ARC, do not call super.
You can do the same thing when you want to disable all the cells, just post a notification with a different name like EditingIsDisabled.
Let me know if you need me to flesh out the code a bit more.
Edit: I like DBD's method better in this situation.

Cancel button click highlight after showing UIActionView on iphone

I have a view that has a button which brings up a UIActionSheet pop up on click. If the user clicks cancel, the original button that brought up the action sheet is still highlighted as if it is being clicked. How do I reset the state after user cancels the action?
Use one of the protocol methods to change the state of the button that was clicked:
#protocol UIActionSheetDelegate <NSObject>
#optional
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet; // after animation
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
#end
Sorry about my false assumption that you were using UIButton. Here should be what you need:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//your implementation here
....
//Then deselect the row so it quits the highlighted state
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Is there any exit editing mode for UITableView?

The flow is something like that... The user see a UITableView, when the user click the "edit", the user can delete the UITableView's cell, after that, he /she click done to confirm. Which method is called when the user click the done key? Thank you.
On UITableViewController it's setEditing: with NO as argument.
By overriding the UITableViewController's setEditing:animated: method, you'll get the message you are looking for.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
// User tapped the edit button
} else {
// User tapped the done button
}
}

Question on edit button on sqlite

I am trying to show minus symbol when I show data from an sqlite database, but if we show an edit button it shows a delete symbol. I am using navigation controller to show data. I want to show when the page loads, it must be with a "-" symbol without edit button? Does anyone have an example or tutorial?
If I understand you correctly, you want to delete the row immediately, without confirmation of tapping a delete button.
First: Users are used to having a delete button confirmation, and if you delete immediately, they may be surprised.
Second: If the issue is deleting many rows quickly, could you instead allow the user to select multiple rows and delete them all at once (like in the email app)
Third: A solution to your question:
Make a custom UITableViewCell and override this function
- (void)willTransitionToState:(UITableViewCellStateMask)state {
if (state && UITableViewCellStateShowingDeleteConfirmationMask) {
[delegate deleteCell:self];
}
}
You will need to also have a delegate property on the UITableViewCell subclass and set it to your view controller whenever you create a new cell. Then add this function in your view controller:
- (void) deleteCell:(UITableViewCell *)cell {
NSIndexPath *idx = [self.table indexPathForCell:cell];
if (idx) {
NSArray *arr = [NSArray arrayWithObject:idx];
//Delete from database here
if (/*delete success*/) {
[self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
}
}
}
Still not sure exactly what you're looking for, but if I understand better this time, you just want the - symbol to show up as soon as the view appears?
- (void) viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}