UISearchDisplayController with UITableViewController - iphone

I have a UITableViewController, in which I added a UISearchBar as the tableHeaderView using Interface Builder. Then I added a UISearchDisplayController in the nib, and set up all the connections (delegate, searchResultsDelegate, searchContentsController, searchResultsDataSource, all connected to the UITableViewController).
I then implemented all the delegate and data source methods in my code.
It works like a charm, except for a weird bug: sometimes the search results table view won't scroll, and I can see the flash indicator of the main table view behind it. I NSLog'd the searchResultsTableView and apparently it's a subview of the main tableview, and I guess that's the reason behind the touch problems I described earlier.
What's my mistake? Is it possible to use a UITableViewController with UISearchDisplayController at all? If so, how do I set up it in such a way that the results table view doesn't get added as a subview of my main table view?
Update: I found this sample which uses UISearchDisplayController with UITableViewController and apparently the search table view gets added to the main table view in there as well. So now I don't think that's my problem.
The thing is that I can't find any substantial difference between what I'm doing and what that sample is doing. I'm just adding an UISearchBar as a UITableView header in a UITableViewController and adding a UISearchDisplayController to it... It's like iOS is confused between the main table and the search table when I try to scroll. Do you have any ideas?
Update: Added a 200 rep bounty. Please answer only if you know what you're talking about.

It was an extra self.tableView.scrollEnabled = YES which was called as soon as the search data request was finished. Hope it helps anyone with the same problem in the future.

A search display controller manages display of a search bar and a table view that displays the results of a search of data managed by another view controller.
Here the searchDisplaycontroller combines the searchbar and tabelview which showing the result data in the tableview so it won't required separate tableview.
While when you using UISerarchBar then it need to have atleast one UITableView, which can show results in the view. And here you can see the result without even adding the SearchDisplayController to view.

I think you might want to try adding a UISearchDisplayController IBOutlet to your class and connect that to your SearchdDisplayController in the nib file. I am amusing that that searchdisplay controller in your nib automatically connected it self to the default serachdisaplaycontreoller outlet in the inspector.

The search bar controller uses its own table view that is added on top of your own. If you set the delegate and dataSource of the UISearchDisplayController to your UITableViewController, then the table view controller has to make a distinction between the two table views in its delegate methods. If you don't do that properly, this may be causing your bug.
In other words:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return number of rows in data model;
} else {
perform the search query;
return number of rows in search query;
}
}
You do something similar for all of the delegate and dataSource methods.
I'm not 100% sure that this is what's causing your problem, but without seeing more source it's hard to say.

Related

adding a static UITableView to a regular custom view

I have a custom view controller that has a view on the bottom half.
I would like to add a static UITableView on the top half.
So I dragged a UITableView on the view controller but apparently that is not allowed since static table views only are only embeddable in UIViewControllers.
I went to my code and made my controller extend UITableView but that doesn't fix the issue.
How do I add my static UITableView as a second view in my custom controller?
EDIT: Perhaps having a table view not taking up the whole screen is not very well supported in iOS storyboards. Maybe I will just use regular tables on a view since i just need 3 static rows.
You should be able to do something simple like this:
UITableViewController *tbv = [[UITableViewController alloc] initWithFrame:[CGRect whateverSize/Location]];
[self.view addSubview:tbv];
Be sure when doing this to also write needed delegate functions such as numberOfRowsInSection: , numberOfSections: , cellForRowAtIndexPath:, tableView:didSelectRowAtIndexPath:
Be sure to check out the UITableView Class Reference
I solved this issue by making my second view controller a simple UIViewController that implements the delegate and datasource, dragging a dynamic table on it and setting the rows and sections "statically" in code. Now I have my two views correctly cohabiting in a large view.

iPhone - UITableViewController and Core Data rows repeating in tableView

I am new to Cocoa and Core Data and I've encountered a weird problem. I successfully created the Core Data model, imported the data, made the UI (Navigation controller, tableViewController with searcDdisplayController) but now I'm stuck at one problem. If I implement a UITableViewController (with no UITableView in the nib file), the results fetched from fetchedResultsController are weird - the row count is correct (500 rows), but they are repeating themselves - only 8 different rows.
However, I was able to fix this problem by adding #synthesize tableView; in the .m file.
Then i encountered another problem - when i click on a row and push the details view, then click the back button on the Navigation Controller, the selected row in the tableView is still selected.
I hope I am being clear on what's wrong. Let me recap:
If I don't put the #synthesize tableView; on the top of the implementation file, the deselection of the row is working fine, but the results are wrong - 8 rows (out of 500) repeating in the tableView.
If I put the #synthesize in the file, the data in the tableView is correct, but there is no animation when i get back to the tableView with the navigation controller.
I also tried to put a UITableView in the nib file of the listviewcontroller, but the results were the same - no deselection of the row. I also tried to deselect row in the viewWillAppear delegate, but the indexPath of the indexPathForSelectedRow is null.
Oh, and I'm using an NSFetchedResultsController. Like I said - the fetched results are correct, but it seems that they're not properly fed to the tableview (if it is not synthesized..)
Thanks!
I might be able to solve your row highlighting issue but you will need to include some code examples so we can help you with the other items...
To deselect a row you can call the method [deselectRowAtIndexPath:animated:] this will allow you to deselect the row before or after you push your detail view controller from your tableView selection delegate. Table View Reference
Thanks for responding!
Hmm..I may have found a solution, but I'm not sure it's correct.. I am implementing the UITableViewController in my new class, but I'm actually using a new UITableView (tableView2) for the data fetching. In the nib file i created the UITableViewController, set the Class name to my custom class BUT I didn't connect the Table View in the Interface Builder with my newly created tableView2. I hope you'll be able to understand.. It's quite complicated, this whole stuff :S
So if I get this right - if I implement an UITableViewController, my class automatically gets a self.tableView? But, like I said in my question, if I don't synthesize the property in the .m file, the controller can't deselect the row automatically..but if I do, the data isn't correct and is repeating..Maybe there is a problem in the cellForRowAtIndexPath, where i get my cells from self.tableView..but It's not logical :S
Anyway, now the row de-selection animation works without my interference and the data displayed is correct. I'm suspecting that the UITableViewController has some other methods that need to be overwritten if using the NSFetchedResultsController.
Do you think my method is incorrect? Having a new UITableView in my custom UITableViewController class?

UItableView + UItabbar

UItableView style not changing. I have a tableView controller as one of the tabs of my tab bar controller. I am not able to change the style of UItableView to grouped.
Please help,
You must specify the Tableview's style upon creation. Either in IB or by using the method
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,416) style:UITableViewStyleGrouped];
I would create a new tableViewController and make sure to enable the "also create xib"-like option when giving the tableviewcontroller a name. Copy paste all youre old tablviewcontroller code to the new one and add the xib to the tab window..
You can probably do that in your UITableViewControllers -viewDidLoad method.
self.tableView.style = UITableViewStyleGrouped;
Edit:
It seems the style property is actually read only, so the above won't work.
It looks like the table view controller has to be created with -initWithStyle:, but I don't know how to do that from Interface Builder. I'm not at my Mac right now, but will have a look later.
Edit 2:
Here's what I did in Interface Builder:
Add an instance of UITableView and set it up as required, including the style
Hook up your UITableViewController as the delegate and data source of the UITableView
Connect the UITableView with the view outlet of the UITableViewController. I'm not sure if there is a tableView outlet - if there is, then probably connect it with that one instead.
So, basically, instead of letting the UITableViewController create its own table view, you provide one in the xib and set up the required connections (delegate, data source, outlet) manually.

What's the best way to refresh a UITableView within a UINavigationController hierarchy

I'm pretty new to iPhone development and have struggled to find what I consider to be a neat way around this problem.
I have a user interface where a summary of record data is displayed in a table inside a navigation controller. When the user clicks the accessory button for a row, a new view is pushed onto the navigation controller revealing a view where the user can edit the data in the corresponding record. Once done, the editing view is popped from the navigation controller's stack and the user is returned to the table view.
My problem is that when the user returns to the table view, the table still shows the state of the data before the record was edited. I must therefore reload the table data to show the changes.
It doesn't seem possible to reload the table data before it is displayed as the call only updates displayed records. Reloading it after the table has been displayed results in the old data changing before the user's eyes, which I'm not too happy with.
This seems to me like a pretty normal thing to want to do in an iPhone app.
Can anyone please suggest the best practice approach to doing this? I feel like I'm missing something.
Cheers - Steve.
The standard approach may sound like a lot of hassle at first, but is a useful pattern for a lot of situations.
In your tableview class create a method like:
-(void)editDone {
[self.tableView reloadData];
}
Add a property to your edit controller like:
#property (assign) id delegate;
Set the delegate when your accessory is clicked:
editController.delegate = self;
And when editing is complete, call your method like so:
[delegate performSelector:#selector(editDone) withObject:nil];
You can create similar methods to handle cancel of your edit component, or to carry out dismissing of modal edit controllers, etc. It's considered more classy to put all this in a protocol, if you like.
I'd implement this in the following way:
Save indexPath of a clicked cell.
Implement -[UIViewController viewWillAppear:] method of the view controller, which contains the UITableView. If saved indexPath is not nil, reload specified cells with:
-[UITableView reloadRowsAtIndexPaths:withRowAnimation:]

UITableView scrolling crashes App

Preface: I am new to the iPhone SDK, Obj-C, Interface Builder and Cocoa. I'm likely doing something obviously wrong.
Question:
I have a UITableView which crashes if I scroll it. It will scroll a little to reveal the full cell of the bottom most half-hidden cell, but won't load the next one. Similarly, if I scroll past the top to fully hide the bottom most cell, and it rubber bands back to show that cell, it will crash before showing it. This strikes me as odd because it is drawing the first 7 of 11 cells correctly. The cell data is in an NSArray, in a UITableViewController linked as both the dataSource and delegate for the UITableView in Interface Builder. It works when the view initializes.
I'm making an App I thought I'd be done with 2 days ago that just calculates combinations and displays a list of them in what I thought would be a convenient scrolling table view. Right now, it doesn't even calculate everything, the NSArray in the DataSource is initialized once with some strings like #"Hello" and #"World".
Steps to reproduce:
Because I'm using IB, I can't exactly show you the full story in code. So I'm going to describe what I did so far and hope it doesn't make you sleepy.
Made a new "Tab Bar Application" in Xcode, because I want 2 tabs, and I don't want a nav bar nor a full screen table. I moved the MainWindow.xib's first tab view out into FirstView.xib as an analogue to the given SecondView.xib. This worked nicely. I modified the view to contain two UITextFields for inputs, and a UITableView for output. This worked but the table was empty. I subclassed the UITableViewController wherein I populated an NSArray property named combinations with 11 strings, and then added
// Set up the cell...
cell.userInteractionEnabled = NO;
cell.text = [combinations objectAtIndex:indexPath.row];
where there had only been the comment. In IB I added a Table View Controller to the FirstView.xib and set it's class name to match the name of this new subclass, and control-dragged the Table View in my view onto this Combinations Table View Controller twice. Once linking the dataSource and once the delegate. Although I get the same behavior if only the dataSource is linked.
This runs and populates the table's visible rows (6.5) with the first 7 values in the dataSource combinations. I can scroll 0.5 cells down, and then back up. But if I scroll more than 0.5 cells up or down the app will crash. The explanation in the report reads:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSCFTimer tableView:cellForRowAtIndexPath:]:
unrecognized selector sent to instance 0x52ca40'
I didn't make an NSCFTimer nor did I link it to my Table View, I suspect that the cellForRowAtIndexPath is exactly a message that should have gone to my dataSource or delegate so I'm confused at why it went astray and where it ended up going.
Update: Thanks for the answers and comments. My problem seems to be that the CombinationsTableViewController (subclass of UITableViewController) is actually not instantiated any particular place in my code. It does get created as some time (when the FirstView.xib is loaded) and is apparently managed while the initial tableView is filled with 7 cells, and is then released. So I need to identify where/how to make a retained reference to this controller. My Application Delegate should probably have some outlet that holds this controller that can be linked as the instance which is in the xib. Yeah, I'm new to this. I know I could just eliminate these troubles by avoiding the IB and doing things explicitly in code, but I figure I want to learn to use the IB flexibly.
Finally: Yes, I needed a retained instance of the table view controller. It sounds elementary, but this wasn't clear when working with the IB as I had. Read my own post for the whole process and fix.
Aside: Either the debugger needs detailed instructions (any links appreciated), or it doesn't work very well. I seem to get more information more quickly by letting the App crash and reading the report it generates. But this requires a tedious termination, relaunch, and 3 clicks. I had really wanted to move on from this to wiring up the inputs, doing the calculation, and updating the table with each change. That's supposed to be the hard part, not this making a framework member work stuff.
Further rambling: This was all in the iPhone SDK for 2.2.1. At the time iPhone OS 3.0 non-beta was not available yet without joining the club for cold hard cash. I expected it to be at the open of WWDC 2009, but it was actually today (July 17th 2009) that the free public 3.0 SDK was made available.
looks like you are losing your tableView delegate.
What looks like it is happening is the UITableViewDelegate is getting released and the app is then using the same pointer address for an NSCFTimer.
Have you called release on your delegete anywhere, or have you not retained the delegate if it is in an autorelease pool.
Okay so, if you followed the steps to reproduce I will now add the steps to solve this:
Steps to fix this problem
Subclass a UIViewController. I called mine CombinationsViewController. In this controller add a property as an IBOutlet for the combinationsTableViewController from step 6 below.
Don't forget to import the right stuff, and synthesize the table view controller, also release it in the dealloc method.
In FirstView.xib change the File's Owner class to this latest subclass.
Link it's combinationsViewController outlet to the Combinations Table View Controller in the FirstView.xib made in step 7 below.
Open the TabBarController in your MainWindow.xib select the first tab, and in the Identity Inspector change the class to the latest subclass (CombinationsViewController).
That makes the table populate normally and scroll stuff.
Now I'm going to move on finally and get some custom table view cell stuff happening and actually make my app do stuff.
Enumerated Steps to reproduce as a reference to the fix:
Made a new "Tab Bar Application" in Xcode.
Opened the Tab View Controller, dragged the prefab view for the first tab out into the MainWindow.xib
Made a new view based xib called FirstView.xib as an analogue to the given SecondView.xib, and put that prefab view into this xib.
Linked the view to the File's Owner's view outlet.
I modified the view to contain two UITextFields for inputs, and a UITableView for output.
I subclassed the UITableViewController as CombinationsTableView wherein I populated an NSArray property named combinations with 11 strings, and then added the cell.text = [combinations objectAtIndex:indexPath.row]; code where there had only been the comment about setting up the cell.
In FirstView.xib I added a Table View Controller and set it's class name to match the name of this new subclass, and control-dragged the Table View in my view onto this Combinations Table View Controller twice. Once linking the dataSource and once the delegate.
At this point the table does render with data, but the scrolling breaks. This is because the CombinationsTableView isn't retained anywhere. And that's very unclear to a first time IB user. So you need to apply the fix listed above.
The first person to summarize this in their answer get the correct answer check mark. E.G. Make a viewController subclass that is the file owner of FirstView.xib and contains an retained IBOutlet you can link to your table view controller in the same xib file.
The reason the first cell loads, is because the tableview pre-loads that like the other cells that are on screen.
All cells are loaded from the datasource method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I would make sure that you have explicitely set the delegate and datasource of the tableView. This can be done in code or IB. by:
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
Also as Bluephlame said, you might be releasing the UITableVewController somewhere.
To find out, set a breakpoint inside the dealloc method:
- (void)dealloc{
//releasing things
[super dealloc];
}
If you do release it, you will hit this breakpoint. Then you can start to track down the culprit.
allocate memory to array instead of [NSArray arrayWithObjects:];. Without memory allocation it wont reload rows....use array=[NSArray alloc] initWithObjects]; ...instead of the above.
I had the same issue and fixed it by adding nil into an array as the last item.
It was crashing for this:
pairs = [[NSArray alloc] initWithObjects:#"EUR/USD",#"USD/JPY",#"GBP/USD",#"USD/CHF",#"USD/CAD"];
but not for this:
pairs = [[NSArray alloc] initWithObjects:#"EUR/USD",#"USD/JPY",#"GBP/USD",#"USD/CHF",#"USD/CAD",nil];
Hope this helps.
I had this same problem. I had a UITableViewController sub-class in a nib file, but I had only declared the UITableView as an outlet/property, which the tableview's UITableViewController was getting released while still in use.
The quick fix was simply to add an outlet to the File's Owner class that referenced the embedded UITableViewController.