UITableViewCell setEditing is not called - objective-c

I have UIViewController that contains UITableView. I implemented my custom edit button that toggles setEditing:animated for the table view. Everything works good, delete and reordering icons show up. Table view cells has custom class (subclass of UITableViewCell), where I override setEditing method to do some extra customisation when edit mode is ON.
From documentation:
When the table view receives setEditing:animated:, it sends the same
message to the UITableViewCell object for each visible row.
As I understand setEditing method should be called for every cell, but unfortunately it does not happen. Where could be the problem? What I am missing?

I found my mistake. I was overriding
-(void)setEditing:(BOOL)editing
but instead I should override
-(void)setEditing:(BOOL)editing animated:(BOOL)animated

Related

UICollectionView selection/deselection methods not triggering

Basically I have a UIPageViewController which has multiple UICollectionView on each pages.
The problem is I can select/deselect on collectionViews inside the first page. But when I switch to second page, numberOfSections, numberOfItemsInSection, cellForItemAt are all called however the selection and deselection delegate methods are not called at all.
here is the link to the code:
https://gist.github.com/anonymous/4eca4ff9f3e4423c01974609aeae5482
I can't connect to your link. But you must make sure delegate of UICollectionView was set and check allowsMultipleSelection and allowsSelection is YES. You can reference links below:
UICollectionView - didDeselectItemAtIndexPath not called if cell is selected
iOS: UICollectionView cell selection not working

Button is disabled when using UITableViewCell instance as if it was a UIView instance

There is a custom view that I extend from UITableViewCell.
In CustomTableViewCell, I implement init method so that it loads a corresponding .xib which contains button.
However, I want to reuse it as if it's a normal UIView instance. For instance, I want add it as a child of my view controller
// in view controller
CustomTableViewCell* customTableViewCell = [[CustomTableViewCell alloc] init];
[self.view addSubview:customTableViewCell];
The problem is, the custom table view cell can be added, but the button that it holds can not be pressed. Meanwhile, the button of the custom table view cell that is populated by table view is working fine.
https://www.evernote.com/shard/s27/sh/59f7b828-f65a-41dc-9156-40a985fceac8/9edb04b8e5c821ad7a46eb714cc776dc
In the screenshot above, the button with the yellow highlight can not be pressed, while the blue can.
Anyone has the idea why it's not working?
It's a guess, but you create the cell with a CGRectZero frame. It doesn't clip it's subviews so they can still be seen, but it's subviews won't get touches outside of the superview's bounds (which are zero). Try alloc initWithFrame:CGRect(/* some reasonable values */).
Once you get that working, you can try with some layout constraints.
May be you need pass fileowner in 'init' method, if you have assigned button's actions to fileowner in xib-file.

Receiving notification on a UICollectionViewCell's (or UITableViewCell's) removal from its parent collection or table view

Background:
I have a UICollectionViewController that shows items in one of two modes, which the user can toggle between. Each mode uses a different class of UICollectionViewCell. Let's call these modes "list view" and "grid view".
When I switch modes, I call .reloadData on the UICollectionView, which redraws the collection view using the correct cell classes. Everything works fine here.
Now: Inside the UICollectionViewCell subclass for one type of cells, I want to be notified when the collection view that contains it switches modes. Visually, a cell which was on-screen vanishes; the collection view is drawn fully with the other type of cell. When switching back, the cell is re-displayed.
Question:
How can I be notified when a UICollectionViewCell is "removed" (i.e., no longer shown; I'm not sure what's happening under the hood yet) from its parent collection view?
Notes:
prepareForReuse is not called on the cell when the collection view's updateData causes the cell to no longer be included.
willTransitionFromLayout:toLayout: (an empty layout?) is not called.
Overriding didMoveToSuperview is of no help; it is not called.
Observing .hidden or .alpha on the cell does not work.
The cell's dealloc is not called; it sticks around in the reuse queue.
Something in the cell must be changing that I can observe or hook into, what is it?
Update: UICollectionViewDelegate has this method, which from the documentation seems like it does what I am asking:
collectionView:didEndDisplayingCell:forItemAtIndexPath:
Original answer:
I got this working as desired by having the UICollectionViewController manually notify visible cells of impending doom with this method when I'm about to toggle and call reloadData:
- (void)notifyCellsWillBeHidden {
for (UICollectionViewCell *cell in self.collectionView.visibleCells) {
if ([cell respondsToSelector:#selector(willBeRemovedFromCollectionView)]) {
[cell performSelector:#selector(willBeRemovedFromCollectionView)];
}
}
}
These cells can then do what they need to do if they implement the above method.
Calling prepareForReuse may not a good idea because it will be called again before the cells are re-displayed, if this is a problem.
I'm still curious whether there is a way for a cell to receive notification that it is going to be made non-visible without an explicit call.

How to enter delete confirmation state UITableViewCell from custom action?

I have a problem, I want to show right delete confirmation button of my UITableView's cell. Swipe to delete works just fine but I also want this to happen without swiping.
I would like to change cell's state from default to showing delete confirmation. There is bool property showingDeleteConfirmation but it's read only.
[self setEditing:YES animated:YES];
From custom cell's class doesn't do anything.
How to change cell's state to showing delete confirmation without swipe?
i.e. after swipe gesture on a cell is recognized what is being called? I would like to call it manually.
That is a delegate method meaning you should not call it yourself as that is a part of the process handles by the tableView class.
Instead you'll want to override the call class, prepare the method to show your button (or whatever you want to show) and animate other views trimming their frames. Doing that you'll understand the difference between the delegate and instance messages as you'll probably need to notify the tableView or it's delegate that your method is complete, the button is shown and other views are also animated.

Combined UITableView with other elements - how to create and use the view?

Ive a project close to doing everything I need it to do. Its got a Main page which has four buttons that allow you to choose an option. Then a tableview page is launched from those options and displays a parsed XML feed from a website. You can then select one of the options in the table to see a detail view of the item, enquire about it, etc.
My problem is I need to add more elements to the TableViewController page, other than the tableview itself. I want a customized back button (not the navigation controller standard) plus some labels, images, etc.
In the TableViewController xib, the tableview itself fills the page. I cant resize it to add more elements above it. I can add a 'view' window seemingly above the tableview and put things in it. But it seems to add the view to the tableview. This means that when I scroll the table, the other elements like new back button, scroll away as part of the table.
So I'm led to wonder whether I need this page not to be a tableviewcontroller, but a viewcontroller, with a tableview inside it, as well as my other view with buttons, etc. Is that the right way to go? But if thats the case, then how do I instantiate the tableviewcontroller within code? Because my page will not be of that type anymore - it will be just a viewcontroller. But in code Im making a tableviewcontroller .. slightly scared by that route tbh.
Any illumination on this would be much appreciated, as various searches have left me none the wiser. Thanks.
To customize it, this is the way to go:
Change your class to be a view controller instead, which implements the TableViewDelegate and TableViewData Source protocols.
In the view didLoad of you controller, create the table view, set its delegate, data source, and any other properties you wish and add it as a subview to your view.
tableView = [[[UITableView alloc] init] autorelease];
tableView.delegate = self;
tableView.dataSource = self;
// .. Other customization
[self.view addSubview:tableView];
I suggest doing this programatically rather than IB.
Instead of a UITableViewController, you want a UIViewController that has an IBOutlet UITableView. Drag and drop a UITableView component from Storyboard and hook it up, and position it as needed on the screen. This UIViewController should implement the UITableViewDelegate and UITableViewDataSource methods.
Edit: Remember that a UITableViewController is just a subclass of UIViewController so you don't really have to get rid of all your code. :) You only need to make minor tweaks to your header and main file, and change the Storyboard to be a UIViewController instead of UITableViewController as I mentioned above.