How can I detect when the user has swiped an editable UITableViewCell? - iphone

I have an editable UITableView. By default the user can swipe and the Delete button will show up. I would like to hide some elements on my UITableView cell when this occurs. How can I do this?

Oh c'mon:
tableView:willBeginEditingRowAtIndexPath:
...
Discussion
This method is called when the user swipes horizontally across a row;
as a consequence, the table view sets its editing property to YES
(thereby entering editing mode) and displays a Delete button in the
row identified by indexPath. In this "swipe to delete" mode the
table view does not display any insertion, deletion, and reordering
controls. This method gives the delegate an opportunity to adjust the
application's user interface to editing mode. When the table exits
editing mode (for example, the user taps the Delete button), the table
view calls tableView:didEndEditingRowAtIndexPath:.
Reference
And then throw some [[cell viewWithTag:<#View's tag number#>] setHidden:YES] for your own views.

Try overriding the willTransitionToState method in your custom UITableViewCell. In particular you would be interested in the UITableViewCellStateShowingDeleteConfirmationMask state.

Couldn't you modify the pertinent elements of the – tableView:willBeginEditingRowAtIndexPath: is called?

As soon as the user wants to editing something in his tableView, this method gets called
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
if(editing){
// Entered Edit mode
// Show the new tableView and reload it.
}
else {
// End of edit mode
// Bring back the tableview and again reload it.
}
[super setEditing:editing animated:animated];
}

Update your UI in tableView:willBeginEditingRowAtIndexPath: and Restore in tableView:didEndEditingRowAtIndexPath:.

Related

Textfields don't get selected after reloadData on UITableView

In my UITableView I have rows which contain 4 textfields each. After user ends editing couple of things in UITableView datasource are being recalculated, so I need to reload UITableView after edition of textfield. But because tableview is being reloaded, when user touches another textfield to edit while editing the first one, second textfield doesn't get selected. He has to touch it again before it can be edited.
Is there a way to avoid this behaviour? Unfortunately I do need to use reloadData instead of reloadSections or reloadRows methods. Any help will be appreciated, thanks!
Reloading a table view will resign the first responder of the text field or even text view. You could try this trick if you have multiple changes that need to be made.
[[self tableView] beginUpdates]
[[self tableView] endUpdates]
Try assigning a gestureRecognizer to each field or same one to all fields. Once you capture the touch in required field, capture the tag and make that field the firstResponder.
Summing up all the help I received and my own ideas I've gone with the following:
-Delay reload for 0.01 sec so textFieldDidBeginEditing gets called
-Then save textfield's tag and cell's index path to variables in textFieldDidBeginEditing
-Call reloadData on tableview
-Call becomeFirstResponder on textfield saved with variables
This however caused keyboard hiding for a brief moment as it didn't have any responder during reloading. To resolve that I created a fake textfield in tableview's superview and assigned him as a responder up to a moment when tableview was reloaded to assign back to the textfield that was selected by a user.
Certainly not the cleanest solution, but it works.

UITableview Cell adding button in delete method

In my UITableview i have option for user to delete the row, i have done that but here i need to add a EDIT button with the DELETE button like the picture,
also when user clicks edit i want to allow the user to edit the ROW TEXT.. is it possible..?
Please help.
First of all you should ask yourself if you really need that edit button.
The user told you already that he wants to edit the tableView by tapping the edit button of the tableView. In my opinion there is absolutely no need for another edit button, the user will assume that everything he does in edit mode will edit the data.
There are two options:
Put the editing in another view controller that you push when the user taps the cell in edit mode.
For this you have to set the editingAccessoryType of the cell to UITableViewCellAccessoryDisclosureIndicator, to indicate that the cell can be selected.
And allowsSelectionDuringEditing of the tableView has to be set to YES.
Everything else is like handling row selections when not editing. Simply check for [tableView isEditing] in tableView:didSelectRowAtIndexPath:.
Replace the UILabel of your cell with a UITextField with a borderStyle of UITextBorderStyleNone and the same font as the UILabel. Set enabled to NO. This way it will look exactly like a UILabel.
Implement setEditing:animated: of the UITableViewController to enable each textField in editing mode
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
for (UITableViewCell *cell in [self.tableView visibleCells]) {
UITextField *tf = ...
tf.enabled = editing;
}
}
when the user taps the UITextField in edit mode he can input text.
If you really need that edit button add it in - (void)setEditing:(BOOL)editing animated:(BOOL)animated when editing is YES and remove it when editing is NO. Instead of adding and removing it I would just use setHidden:
Yes it is possible. You'll have to use the UITextField to show the text.
When user click on the Edit button you'll have to save the tag of the button. Save this tag so that you know which row is clicked. And then reload the table. While reloading the table make sure you set the userInterActionEnabled property of the UITextField at the particular row to true. It'll allow user to edit the text.
Create Custom cell and add your UIButtons and UITextField and just implement some logic. just refer tutorial from below links.
For Custom cell just see these below links with tutorials and demos:
custom-table-view-cells-in-ipad-programming
creating-custom-table-view-cell
Custom UITableViewCell in IB

iOS: cancel delete event UITAbleViewDelegate

I want to know the delegate method that gets called when the user cancel the delete operation of the UITableViewCell. Now to answer the potential question that "Why do I need this?", following is the scenario:
I have a table view where the item is displayed (which is left aligned to the main view) in a UILabel and its price is displayed in a separate UILabel (which is right aligned to the main view).
Now once the user presses the red (kind of no entry) button to delete any item, the whole cell is indented to the left and half of the price is clipped because of being out of the view. This looks quite ugly and hence I hide the price label upon press of this 'pre-delete' button (which works fine). But I want to display the price tag back when the user dismiss the delete button without deleting the cell. but I am unable to find the cancelDelete kind of event for tableview cell.
Thanx :-)
Here's something that might work instead, it stops the tableview from indenting the cells while editing:
Set UITableView's shouldIndentWhileEditing property to NO.
Implement the delegate-method tableView:editingStyleForRowAtIndexPath: method:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
Or you can do this when you create your cells:
cell.shouldIndentWhileEditing = NO;
Hopefully this works for you.

Change the setHighlighted for UIButton

I am using an UIButton to show and toggle a table-cell-item from read to unread.
I used a blue image as a background to show when an item was unread, and that worked, but figured that it would be better to use the setHighlighted feature, as it saves adding an extra image to the App. The code I use is as follows:
- (void)updateReadButton{
if(article.read.boolValue){
//[readButton setBackgroundImage:nil forState:UIControlStateNormal];
[readButton setHighlighted:FALSE];
[readButton setNeedsLayout];
} else {
[readButton setHighlighted:TRUE];
[readButton setNeedsLayout];
}
}
This works fine for the initial creation of the cell. But when the item is clicked and the detail-view is shown I toggle the 'read' value to 'true' and change the setHighlighted option to false of the UIButton in the cell, but it doesn't change when coming back from the detail view. Only when the cell is scrolling off the screen and recreated is the change reflected. How can I force a redraw of the button once I navigate to the detail view?
You may be able to accomplish the highlight by simply reloading the cell in question. Peek at UITableView's - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation and use it to reload the cell after setting the highlight.
I don't think the method setHighlighted: is the correct one. Here is the documentation:
Specify YES if the control is
highlighted; otherwise NO.
By default, a control is not
highlighted. UIControl automatically
sets and clears this state
automatically when a touch enters and
exits during tracking and when there
is a touch up.

Redoing UITableView layout when user taps Edit button?

I have a UITableView with complex content. The user can edit (rearrange and delete) the cells when tapping the Edit button the standard way. But I want the cells to look different in "edit" mode.
Question:
How to change the UITableView Layout in edit mode, including changing row height?
So far, this is what I have:
The Edit button sends a WillTransitionToState/DidTransitionToState message to each uitableviewcell (UITVC). I have subclassed UITVC and react to these inside each cell, hiding and removing and reshuffling as needed. But, changing the row height is beyond the scope of one cell.
There does not seem to be a message sent to UITableView when user taps edit. There is a - tableView:commitEditingStyle:forRowAtIndexPath: sent to data source after editing a particular row.
Inside heightForRowAtIndexPath, I can query the current mode using the tableView.editing property, and report height as appropriate. And I can trigger re-flowing the table, including recomputing the heights, by invoking [tableView reloadData]. But, when do I call it?
I could send messages from the cells from within WillTransitionToState back to the "owning" table view, and call reloadData when I get them. But this sounds fragile and there must be a better way.
Rhythmic is right. Using reloadData kills the nice editing animation.
This problem is addressed in this post:
Can you animate a height change on a UITableViewCell when selected?
Instead of using reloadData, do the following after calling setEditing:animated.
[tableview setEditing:editing animated:YES];
[tableview beginUpdates];
[tableview endUpdates];
If you wish for your table cells to change their format in response to whether or not the table is in editing mode, you could override -setEditing:animated: in your UITableViewController and trigger a reload (via -reloadData) of the table view on a change of editing state.
Within your UITableViewController's -tableView:cellForRowAtIndexPath: method, you could check for whether or not the table was in the editing state by querying the editing property on the table view, and then return a different cell type depending on which state the table is in.