Difference in UITableViewCell willMoveToSuperview: behavior between iOS 5 and iOS 6 - ios5

Under iOS 5, it seemed that a UITableViewCell subclass's willMoveToSuperview: method was called every time the cell was used or reused, but under iOS 6, it seems that it's only called when the cell is initially created and used, not when a cell gets reused. Can anyone verify this difference? Is this a bug?
What method should I be using to do cell setup inside the UITableViewCell subclass that will get called when the cell is reused in both iOS 5 and iOS 6?

I've been having the same issue with UICollectionView. It seems you have to implement the method prepareForReuse in the view you are using for display (ex. of type UITableViewCell or a UICollectionViewCell). However, this also gets called even if it is the first time creating a cell (not just when it is being reused) if you are using the new dequeueReusableCellWithReuseIdentifier:forIndexPath: with a registered class with registerClass:forCellWithReuseIdentifier: in iOS6.
Reference: UITableViewCell Class - prepareForReuse

1. How to know if a cell is removed from tableview?
use this method to do something, e.g. cleanup, stop timers or something else...
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
2. how to know if a cell is going to be displayed
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
3. how to know if a cell is going to reuse
override this method
UITableViewCell:
- (void)prepareForReuse();

Related

what is the difference between didselectrowindexpath and willselectrowindexpath in iphone?

Give an explanation about difference between UITableView delegate methods:
didDeselectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
and
willSelectRowAtIndexPath
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
willSelectRowAtIndexPath message is sent to the UITableView Delegate after the user lifts their finger from a touch of a particular row and before didSelectRowAtIndexPath.
willSelectRowAtIndexPath allows you to either confirm that the particular row can be selected, by returning the indexPath, or select a different row by providing an alternate indexPath.
Good luck
T
the code written in
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath(NSIndexPath*)indexPath
method is run after selected the row and the code is written in
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath(NSIndexPath*)indexPath
run just before the row selected.
they are same as
- (void)viewDidAppear:(BOOL)animated and - (void)viewWillAppear:(BOOL)animated
let me know if you have any confusion now.

is there any if statement for showing/hiding a method in objC?

i use same uiviewcontroller's instance in different tabs.
there is a uitableview in viewcontroller.
in firstviewController instance, i dont wanna edit the uitableview.
in the second one i use edit mode for tableview.
thats why i want to show or hide this method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
is it possible to make an if statement like this:
#if (editingOK)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
....some codes
}
#endif
editing OK is a BOOL property.
if you ask why i want it, because if the user swipes on the cell, it display Delete button.
i just want it if my editingOK=YES.
The #if/#endif syntax is used for conditional compilation: it lets you modify your program at compile time based on build configuration. Read about the "C preprocessor" to learn more.
If you are, as you say, using the same object instance as the delegate of different UITableViews, you must have some way to determine which table you are dealing with.
What you need to do is implement an additional method:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
That method is called when the user swipes the cell, and you can decide if a delete button should appear or not, then return the appropriate UITableViewCellEditingStyle constant.
Isn't editability controlled by calling the setEditing method of the UITableViewController? So you could set that depending on whether or not you want to enable editing, w/o this #ifdef ugliness.

Deleting custom cell in Table View

I am using a Table View Cell to draw custom cell in table view. Now the problem is that when I try to delete the cell in editing style UITableViewCellEditingStyleDelete only the
the little red -ve sign bitton appear and when I click it nothing happen.
Please help me and tell me how to delete custom cell in table view
Thanks
Gyani its not at all possible that when you click on (-) sign nothing happens.
May be your tables user interaction is disabled. Try enabling tableView.userInteractionEnabled=YES
And still it does not work then post some code.
hAPPY cODING...
did you implement the appropriate delegate methods? Specifically
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Did you implement this delegate method by following form?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// remove the cell from UITableView
[tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:YES];
// remove the data from self array or something else.
...
}
}
Do you have a subview in the cell that stops the user interaction? maybe located in negative x, so it is over the delete button.

tableview has nothing?

my tableview has the following method.i have done everything perfectly...numberOfRowsInSection returns 10.....the following method will not be called....it shows nothing......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
There are lots of places you could have gone wrong...and if you post some code you're much more likely to get a solution but some possibilities.
Have you also implemented
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Have you set the tableview's datasource and delegate to your controller? (Either programatically or via interface builder outlets)
Does your controller declare the UITable view datasource and delegate protocols in it's header OR subclass UITableViewController?
Without you providing more info it's pretty hard to give a solution.

iPhone Can't deselect a UITableViewCell

I have a RootViewController class which is inherited from UITableViewController.
When a cell is deselected by the user I want to enable/disable certain buttons on the toolbar.
How do I trap the deselect event?
-(void)tableView:(UITableView *)tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
if(indexPath.row <= rowNumber)
{
[viewButtton setEnabled:NO];
[editButtton setEnabled:NO];
}
}
I tried using this method but it doesn't seem to execute at all. Any ideas how cam this be done?
I do not think there is a deselectRowAtIndexPath event, there is a method that you can call to deselect the indexPath, but looking at the SDK I do not see an event for this in the UITableViewDelegate: http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html.
Could you enable/disable certain buttons on the toolbar during the didSelectRowAtIndexPath: event?
-Rog
This is in the current beta SDK only which means it could be buggy / changed / unsupported...
I did noticed that your method declaration doesn't match the SDK (at least, the version I have).
Try removing animated:(BOOL)animated; I don't think it's applicable here.
See line ~345 in UITableView.h, and/or right click on didDeselectRowAtIndexPath and "Jump to Definition", where you'll probably find how the delegate method should be defined.
That said, if your goal is simply to "enable/disable certain buttons when a cell is selected",
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
should work just fine. This will occur after they select the cell and before it's deselected. 'deselect' has to do more with animation than user interaction. The only reason I can think you would want to use deselect is maybe the aesthetic value of ensuring your event only occurs after the select cell is no no longer highlighted.