When editing UITableView how to prevent that delete button from poping out? - objective-c

Usually when user click delete button in edit mode of UITableView a delete button will pop up asking for confirmation.
I don't want that.
How do I get rid that confirmation ritual?
I want my delete button to work like the plus button. Just add. Just delete. No need to confirm. At least not when the stake is low like in one issue of my program where deleting will simply flag the table to be invisible when out of edit mode.

Delete confirmation popup is not built into UITableView or UITableViewCell (swipe to delete functionality). Such a thing has to be manually added, therefore getting rid of it is easy: don't add a popup :)
If you are maintaining someone else's code, search for this method:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return isEditable ? YES : NO;
}
Method documentation says:
The method permits the delegate to exclude individual rows from being treated as editable. Editable rows display the insertion or deletion control in their cells. If this method is not implemented, all rows are assumed to be editable. Rows that are not editable ignore the editingStyle property of a UITableViewCell object and do no indentation for the deletion or insertion control. Rows that are editable, but that do not want to have an insertion or remove control shown, can return UITableViewCellEditingStyleNone from the tableView:editingStyleForRowAtIndexPath: delegate method.
Here's a link to Apple docs Inserting and Deleting Rows and Sections for more complicated cases.

What is your data source? If it's an array, why don't you remove the selected item from the array and then refresh the uitableview?

Related

UITableView Cell - Edit?

Is it possible to use a UITableView to be able to enter a value into a database's field.
For example, if I was to have a UITableView pointing to a field within a database and if I wanted to enter a new entry into the database - tap on the UITableView Cell that would then allow keyboard input into the cell which ultimately end up being a new record in the database??
This is possible, but if something is possible doesn't mean you should be doing so.
You might ask why?
Well! you are trying to input data from view directly to database, this is a very bad practice. There are many reason for it being bad, the major is efficiency and security reasons.
You should consider using MVC pattern.
Now since its completely possible, I will explain the idea on how to do it and conclude with links that will have real code examples.
In tableView:cellForRowAtIndexPath:
add a TextField with tag (to get the reference back in future) and add it to contentView of the cell and have it hidden.
Now in tableView:didSelectRowAtIndexPath: make the cells editing property to YES.
Then, in tableView:willBeginEditingRowAtIndexPath:
get the reference to the textfield in contentview using viewWithTag: method and hide the textLabela and unhide the textfield.
In textfield's delegate textFieldDidEndEditing: make cell's editing property as no (yea, you need to keep the reference) unhide the textlabel and hide textfield.
In tableView:didEndEditingRowAtIndexPath: write methods which will commit the changes to your db.
Below are list of links which will get you code examples:
Having a UITextField in a UITableViewCell
Accessing UITextField in a custom UITableViewCell
iOS Database Tutorial
There are no examples for your requirement 'coz it bit bad way of doing things.
Yes its possible....
You can use delegate methods to take data form you cells textfield to your parent view controller and then save data in database.

UICollectionView delete button

I have a UICollectionView showing several items. I also have an edit button in the screen toolbar.
How can I have delete icons appear on each UICollectionViewCell when I press the toolbar edit button?
There is very little in the way of examples on google at the moment, so if anyone can point me in the right direction, that would be great.
Thanks
Editing items in UICollectionViews aren't done the same way as they're done in a UITableView. In table views, there's a editing mode that'll show the delete button. But with collection views you gotta take care of that yourself.
Initially, I solved it this way:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.editing) {
// Open an action sheet with the possible editing actions
}
}
But after that I removed the edit button and added a UILongPressGestureRecognizer to the UICollectionView. When long pressing an item I show the UIActionSheet that shows the possible actions.
One of those options might be a possibility for you.
Your code defines what a cell in a collection view looks like. You can add a delete icon/button to the contentView of the cells that your data source provides.

UITableView: How to revert a user input

I have a UITableView with a couple items and I've added a cell that says "No More Items" to the bottom of the UITableView list. I'm preventing the "NMI" cell from being re-ordered, but I can still take one of the other items and drag it underneath the "NMI" cell.
When my view tells me: tableView:moveRowAtIndexPath:toIndexPath, I don't modify the model and reorder the array of items. However, even if I don't modify the model, the UITableView still reflects the new order.
What I'm looking for is a method to reject the UITableView's change. So when tableView says "hey the user is moving this row to the end of the list, you should update the model," the controller can respond and say "wait, that row can't be moved to the end of the list. don't do that"
Try using this delegate method to check if reordering is allowed or not.
- (BOOL)tableView:(NSTableView *)tableView shouldReorderColumn:(NSInteger)columnIndex toColumn:(NSInteger)newColumnIndex

Query UITableView for selected cells in order to display "Delete" button

Background:
I have a UITableView that displays data from an array. I am utilizing KVO to display an "Edit" button in the navigation bar when the array count is greater than 0 and removes itself when the count is 0. This is working fine. When editing, I place a "Delete" bar button item as the right bar button item. This is also working fine. The Delete button as expected is enabled by default. The table view has multipleSelectionDuringEditing set to YES. I am able to select multiple items and delete them properly. My goal is to enter editing with the Delete button disabled until at least 1 item is selected and subsequently disabled when there are 0 selections. I have tried some things but they have been fruitless. I attempted to have an NSMutableSet property (private) that I could monitor with regards to it's count and appropriately enable/disable the Delete button. However, I have run up against a wall. tableView:DidSelectRowAtIndexPath: is only called on an initial selection during editing, NOT when the row is deselected. Am I overlooking the ability to monitor table selection during editing mode? Thanks in advance.
Additonally, if I were to move the "Delete" button to a UIToolbar with the intention of also including a "Delete All" button, is there a way to select all of the rows (even those not visible)?
According to the docs, calling -indexPathsForSelectedRows on the table view can tell you how many are selected right now, and your delegate's -tableView:didDeselectRowAtIndexPath: should be invoked whenever the user deselects a row.
I don't see a convenient method for programmatically selecting multiple rows. If you're doing a "delete all" command, it might be more appropriate to just delete your underlying model objects and then tell the table to update (that is, not worry about selecting them all first). You can animate that using -deleteRowsAtIndexPaths:withRowAnimation: and/or -deleteSections:withRowAnimation:; either way you'll have to enumerate for yourself which rows/sections you want to delete, but the correspondence between table sections/rows and your collection(s) of model objects should be pretty straightforward, especially since you're nuking everything.

swipe to delete when already in edit mode

I have an iphone app using a uitableview where I'd like the "reorder" controls to always be displayed, and have the user swipe to delete rows.
The approach I'm currently taking is to put the tableview in edit mode and allow selection in edit mode
self.tableView.editing = YES;
self.tableView.allowsSelectionDuringEditing = YES;
I then hide the red delete circles using
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
I cant figure out how to get the swipe gesture to bring up the "delete" on the right side when the tableview is already in edit mode, can somebody point me in the right direction?
alternatively, if someone knows how to get the cell reordering controls to show when NOT in edit mode, that would also be a workable solution
cheers
When the user swipes on a given row, you need to store a reference somewhere so that you can change the value returned by editingStyleForRowAtIndexPath and shouldIndentWhileEditingRowAtIndexPath. Your best bet is likely to use indexPathForCell on the cell that is swiped and store that. Then in the two display methods above you need to check if the NSIndexPath is the same or not (I'm not sure if they will be the same pointer or if you'll need to compare the section/row values - testing required). If they match, display the delete button.
Note that you may need to call reloadData on your tableView to have the effect appear without scrolling away and back again.
I'm wondering if the way you're headed now would break the Human Interface guidelines, which would result in the app not getting approved.
I can't see why you can't capture the swipe gesture and then use that to 'unhide' the red delete (stop sign) icons for the delete confirmation?