UICollectionView delete button - objective-c

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.

Related

Can't edit custom cells with UITableView as subview

I have a view controller that I have a static view up at the top with a button and a tableview that is under it. I'm attempting to create custom cells in the tableview, but I cannot add labels or anything to the cell. Storyboard only allows me to add the labels to the original view or the tableview (but not the cell). How can I get around this?
Here's a picture for reference.
Out of pure "trying everything" I got it fixed by manually dragging a cell into the view and not using the auto generated prototype cells by xcode. It looks like it was a bug with xcode where it's auto generated prototype cells would not let any interaction happen. To fix it, I dragged a UITableView cell from the object list into the tableview and I was able to edit that just fine. Hope this helps someone else out.

Forward horizontal swipe events on UITableView to parent view

I have a UITableView that I want to have respond to taps and vertical swipes, but still have something like userInteractionEnabled = NO for horizontal swipes. By that I mean, it would not handle touches and pass the touch event back to its superview.
Things I've tried that didn't work:
returning NO in - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Overriding touchesBegan/touchesMoved/touchesEnded and passing the event to the next responder
Adding gesture recognizers for horizontal swipes and setting cancelsTouchesInView to YES
I've been trying to fix this on and off for several weeks, so any help is greatly appreciated!
better you can subview empty view for tableview.view color should be
empty and add gesture to that view.if gesture direction is
vertical.scroll tableview
Did you try just adding a horizontal swipe gesture recognizer to your table view's parent view? Also turn off horizontal scrolling on your table view. (set alwaysBounceHorizontal=NO)
Disabling user's interaction for each table view cell should help.
I suggest doing it in cellForRowAtIndexPath method.
The answer to this question is not simple, because UITableView manages a lot of touches, and this touches are managed by different components.
So, to give you a correct and working answer, you have to explain:
what kind of swipes you want to disable (left-to-right, right-to-left)
where (on the empty table view, on the cell)
what happens now when you do this swipe (the cell goes in edit mode, a navigation controller goes to the previous page ecc....)
For example: the swipe-to-delete on a UITableViewCell can't be avoided by overriding touchesBegan:withEvent, because the touch is received by the internal content view (UITableViewCellContentView private class, so you can't subclass and override touchesBegan:withEvent).
But this is not necessary, because you can disable this behavior adding this method to UITableViewController:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
When you disable the editing (for example) the cell and the table view are no more capable of managing the touch, so the touch is forwarded to the next responder in the chain.
This is only one example, you must explain all the cases that are giving you undesider behavior to let us help you more.
Last suggestions:
At this Page you will find a very good and interesting explanation on how the hit test end event handling process works.
I'll update the answer with more specific information when you'll add more details.
Regards
Fabio

Prototype button in StoryBoard?

My app dynamically generates buttons. When the button is clicked it creates a table linked to it. I have all the table interfaces (navigation controllers, etc.) set up in storyboard. However, my main view is the home screen where the user creates buttons. Since the button isn't already there, how do I set it up so that it creates a new instance of the tableview outlined in my main storyboard? Is there a "prototype button" in storyboard? The "#selector" action for the generated button is pointing to this IBAction:
-(IBAction)generateTable:(id)sender {
}
Can someone help me fill in the remaining code?
Also, the home screen view controller is already set up in Storyboard, with the arrow next to it.
EDIT: this picture might help understand my problem a better.
http://i.imgur.com/MdT4i1P.png
Found the answer myself quite easily. All you have to do is make a segue (with an identifier) in Storyboard from your ViewController to the ViewController you want it to connect to, then in the IBAction, just add the following.
-(IBAction)generateTable:(id)sender
{
[self performSegueWithIdentifier:#"GoToTable" sender:sender];
}

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

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?

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?