UITableView didSelectRowAtIndexPath called twice - iphone

Under certain circumstances, UITableView didSelectRowAtIndexPath is being called twice causing the error Pushing the same view controller instance more than once is not supported.
Here's are the sequence of events:
TableView::didSelectRowAtIndexPath.
TableView::viewWillDisappear.
PushedViewController::viewWillAppear.
TableView::didSelectRowAtIndexPath.
Error: Pushing the same view controller instance more than once is not supported'
The only thing worth noting is that the UITableView is loading images asynchronously, but that never calls didSelectRowAtIndexPath. Also, the PushedViewController is reused to avoid having to reload it each time a cell is selected in the UITableView.
Anyone have any idea what may be causing this?
Thanks.

I'm seeing this problem too, probably one out of a 1000 users gets affected, or less. I сan clearly see two didSelectRowAtIndexPath registering 50 ms one after another. My guess is that it is a bug in iOS - no new taps should be directed to old view once new view-controller has been pushed. Alas, it is likely up to us to write code guarding against this. Here's what I'm thinking:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (self.navigationController.topViewController != self)
return;
... do other stuff
}

Disable user interaction after the first "didSelectRow". It's possible for multiple taps to "stack up" during the transition.
It usually takes someone with amazing dexterity in their fingers to get this behavior, but still.

if you already created Storyboard Segue don't call;
[self performSegueWithIdentifier:#"TYPE" sender:self];
in this method;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

This occurs because of the segue you are using inside the didSelectRowAtIndexPath method. Ensure that this segue is created by ctrl dragging from the view button on top of the source view controller to destination view controller. The error occurs when you create the segue by ctrl dragging from the table view cell to destination view controller.
You do not need to change any of you code. Just delete the segue and create it in the correct way. Hope this solves your problem, if I got the question right.

Swift 5
In my case also helped to update the cells instead of this code:
tableView.reloadRows(at: [indexPath], with: .automatic)
to use this:
tableView.reloadData()

Related

iOS - UITableView delegate not being set?

The UITableView object I have in my storyboard theoretically should have its delegate set, but it does not. I dragged the UITableView object from storyboard into the header and added it as an IBOutlet property and synthesized it. However, I checked and only the data source method is being called. So something seems to be wrong with the way I'm implementing the main delegate protocol. As you can see in the images below, I seem to be doing everything standardly? But the delegate is not being set still! Thoughts?
I think the reason your solution is not working is because you are using a TableView inside of a UiViewController instead of a UiTableViewController. I had this same issue a while back. Here is what I did. Create an IBOutlet to the header file and synthesize it in the implementation file (I believe you have already completed this step). Go back to the storyboard. control + click on your table view and drag the connector to the view controller (the yellow circle with the white box). Select datasource. Repeat this step again and instead of selecting datasource select delegate. In the menu on the right-hand side you should be able to see your outlets if it is set up correctly. This should fix your delegate problems.
See the screenshot from my example below:
Try changing your numberOfRows method (for better debugging).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rows = [allParties count];
NSLog(#"number of rows: %d", rows);
return rows;
}
If you're returning 0, it won't work.
Also in the future, please paste your code instead of taking screen shots.
You have put a log in both numberOfRowsInSection: which is being called but not the one in cellForRowAtIndexPath: which shows that no cells are being created for your table. You simply are returning 0 as number of rows for the table. Check for that.
And the delegate method didSelectRowAtIndexPath: will be called when you select a row, but for that, you have to have a row first in your table.

Change a tableview entry

Say I have an iPhone app using a tableview. As shown below, if I click the edit button I go into edit mode. Then I have the option to delete or add. So far so good, and all of my code is working. However, what about modifying one the the cells?
The first cell, for example, is Chevy. Say I want to change it to Ford. How can I edit a cell's value while in edit mode?
Is there another button I can add called "Change"? If so what would the code be for this?
Having looked around Google I can't find any results for this type of question. Does anyone know any good tutorials? Or at the very least, can someone provide some ideas and perhaps code samples to get started?
Depends how you want the user experience to be. One option would be to use the TableView delegate method didSelectRowAtIndexPath to take the user to another page with a text field and a save button.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIViewController* myEditCarDetailsViewController = [[UIViewController alloc] init]
[self.navigationController pushViewController:myEditCarDetailsViewController animated:YES];
}
Once the user clicked save, you just need to update the datasource and call reloadData on the tableView.
Another option would be to make a custom tableView cell that has a textField in it that you set to editable when the table is in edit mode.
I'm guessing you were hoping for an in built apple way. I don't know of one, but it may exist

iphone - scrollRectToVisible issue

This functions perfectly, but I want to make it an once-function, not fixed-function. When I change tableview with other data, the data displays at the index from previous tableview. So my solution to this is implementing the code below. When I implement this, it works, but when I scroll down, it scrolls up all the time, so it is virtually impossible to scroll down further. Any idea how to make it performs only once when I change tableview?
The code:
[tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
Edit 21 august:
Guys, thank you very much! The code was in cellforrowatindexpath. I moved it to inside the function which changes tableview and it works like a charm :D
You could override the reloadData method if that is how you are reloading the Table View with new data and put the code in there. Something like this in your table view controller should suffice:
- (void)reloadData {
[super reloadData];
[tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
If it's scrolling up every time you scroll down, I assume you put the code in the cellForRowAtIndexPath method which will get called every time you want to present the cell. This is not the correct place to put that code.
It IS a once-function. Most probably, this code of yours is executing again & again. If you have kept this in a function such as cellForRowAtIndexPath:, which is called frequently, that may be the cause of this problem. Where have you put it?
HTH,
Akshay

iPhone - Update current view before pushing new ViewController on UITableViewCell selected

I have a UITableView with a list of items. On selection of an item I'm navigating to a new view that displays the details of the selected item using:
DetailsVC *detailsView = [[DetailsVC alloc] initWithNibName:#"DetailsVC" bundle:nil];
[self.navigationController pushViewController:detailsView animated:YES];
Now, the details view is getting the data from the remote location so on slow connection it can take a few seconds. What I want to do is display an activity indicator over the selected row on selection.
The problem is the display of the added indicator gets delayed until the next view is ready to navigate to, which makes the indicator usless.
I've tried to add this indicator in those 2 events with the same effect:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Is there a way to add the indicator (or more general, modify the content of a UITableViewCell) in the moment of selection, before navigation occurs.
As a experiment I've also tried to pop up an alert view in the same two events which resulted in poping up the alert after navigation to the details view.
So it's [[DetailsVC alloc] init...] that takes a few seconds, right? This problem is that any view changes you make don't fully take effect until returning all the way back to the runloop, so even if you setup the indicator before creating your object, it too is waiting for the init to finish to make itself visible. What you need to do is defer the creation of your DetailsVC until after the indicator is setup.
I might be a simpler change to use blocks but I can't recall the details of that off the top of my head (haven't used blocks much since code I've been writing lately has had to stay compatible with 3.x). But to use performSelector is easy too, take those 2 lines you first quoted in your question and put them into its own method, such as:
- (void)pushDetailsView {
DetailsVC *detailsView = [[DetailsVC alloc] initWithNibName:#"DetailsVC" bundle:nil];
[self.navigationController pushViewController:detailsView animated:YES];
}
And where you had those lines before, setup your indicator and then do this (a delay of 0 doesn't mean to called it immediately, but rather ASAP after returning all the way out of the current call stack):
[self performSelector:#selector(pushDetailsView) withObject:nil withDelay:0]
you can call cell = [self rowForIndexPath:indexPath] in one of your functions. and then add the indicator to cells subview
You can either
add the indicator when u select row and push new view when data came
(simultaneously remove indicator at that time) or
Push new view, call web service to get corresponding cell data (in
viewDidLoad or viewWillAppear) and display indicator simultaneously
and when new data comes remove indicator

Problem reloading UITableView

I have a UITableView which is populated by an array, I have a button on the navigaton bar which (when pressed) adds an item to the array and calls [self.tableView reloadData] in the UITableView. This results in numberOfRowsInSection being called and returning the correct number of rows (the number of items in the array) BUT doesn't call cellForRowAtIndexPath.
I have created a new navigation based application to try and find a solution but have exactly the same problem!
If anyone knows the answer it would be greatly appreciated, i've been tearing what's left of my hair out for the last day!
I have put the source for the test project up on my site at www.sofaracing.com/Downloads/Test3.zip
Works for me! ;)
In MainWindow.xib, You added an object Root View Controller (that's not part of the Navigation Controller) - Delete it, not necessary. Then connect outlet refreshFriendsList to the Bar Button of the NavigationItem of RootViewController. Wha-la, magic!
BTW: You may need to clean up the warning. And you might want to think about creating a class for your data model instead of using UIApplication sharedApplication.
I quickly debugged your test app. I couldnt't spot the root cause, but it seems that you have two different table views due some mess up in Interface Builder setup.
If you initialize original array with an item, cellForRowAtIndexPath is correctly called. If you examine self.tableView instance in this call and later in subsequent calls to refreshFriendsList, self.tableView points to a different instance.
2009-05-17 14:33:07.591 Test3[33580:20b] cellForRowAtIndexPath 0
2009-05-17 14:33:07.594 Test3[33580:20b] self.tableView: <UITableView: 0x52b9b0>
2009-05-17 14:42:36.810 Test3[33762:20b] numberOfRowsInSection: tableView <UITableView: 0x53bcd0>