When is cellForRowAtIndexPath called? - iphone

I have a tableview in my app...The contents of this list view are displayed based on ext API dynamic search...
e.g. If I type "G" in searchbar it will display GA, GB, GC1, GC2, GC3,..
if I now change to GC, it will only display GC1, GC2, GC3..
I am storing the items in an array..
My question is how do I ensure that what the user is typing and seeing as search results are synchronized..I mean if he clicks on GC1 in list view, he should see GC1's detail and not other's..
Also when is cellForRowAtIndexPath called so that I can update the array each time user types in searchbar?

-cellForRowAtIndexPath: is sent by the table anytime it wants a cell to display, such as when the table is redrawn, or when a new cell is needed because the user scrolled a bit.
You can force the table to update its contents by calling [table reloadData], if 'table' is a pointer to your table.

You should be updating your array of filtered items in the filterContentForSearchText: scope: method. There is another method, searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString that will be called when the user types in the search bar. If you return YES from this method, then the table will be reloaded, which causes cellForRowAtIndexPath: to be called.

Related

How to use UISearchBar to search only the sections titles of a UITableView?

I have an UITableView with sections and many rows.
I don't need a detailed search, going through all cells in the tableview.
All the sections titles are contained inside a NSMutableArray.
Is there a simple way to search only the sections titles of an UITableView, disregarding the cell contents?
Thank you!
you have to insert the uisearchbar on the head of the table. next you have to implement a method that search on the array with the titles and create a new array with the elements to show (titles and cells, they can also be two array, you have to choose basing on the normal app-workflow) and reload the table.
See this Apple Sample implementation - notice the function named:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
Here, instead of:
for (Product *product in listContent)
You can put:
for (NSString *sectionHeader in YourSectionArray)
Also change implementation of this function as per your need, you should be done. (like, you can ignore the scope things as you only want to search for section header which is nothing but a simple string) This function is simply search routine which you can change as you wish. It is actually called by following delegate function - this you do not want to change:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
Note that this control I am suggesting is an in-built tableview control that comes with search bar already. It is:
UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
If you added search bar on your own, you would need to add both these delegates to your UITableView .h file like I did above. If you would rather copy Apple's example implementation, no worries.

iphone Table View Cell For Row not being called when Rows For Section is returning > 0

Using table views in a view controller that is being used as sub views to another controller. They are displaying fine when first created, etc. Selections are working fine, etc.
But when trying to change the display items in the table, the datasource array is being changed and a reload is done on the main thread. The Rows in Section is being called properly with the correct size of the array being returned, etc. BUT cell for row is not being called.
Here is the sub view setup...
[self addChildViewController : SQLTableNames];
[self.view addSubview : SQLTableNames.view];
[SQLTableNames didMoveToParentViewController : self];
Where SQLTableNames is a ViewController with tableview(s) in them
have tried everything, delayed reload, reload on main, etc. Delayed from the main view controller, delayed in the sub view controller, etc.
It's seems like the return from rows in section is simply being ignored.......
Have tried set need display, and Reload Rows at Index Paths. None of these will call the Cell for Row to repaint the table.
All the delegates are fine. It display's once fine and then can't change the table display.
If you called [self.tableView beginUpdates] before your inserts, ensure that you call
[self.tableView endUpdates];
after.

UITableView datasource method does not call in Device 3G iOS4.0

When I call this Code
[[self tableView]reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:kSectionShoppingList]] withRowAnimation:UITableViewRowAnimationNone];
HeightForRowAtIndexPath Calls
But in CellForRowAtIndexPath for section 3 row 0 does not call.
Looking for your suggestion.
and it is specific to Device 3g iOS4.0.1.
Is anyone earlier have faced same issue.
Is the cell you are reloading visible ?
Not sure about iOS4.0.1, don't have it installed to test, but on iOS5+, the cellForRowAtIndexPath is not called if the cell is not visible, just tried it in one of my apps.
The issue i have mentioned look like a bug in iOs4.0.1. anyway I have solved it by reloading whole table using reloadData method instead of reloading particular rows.
Make sure your are reloading sections and rows that are visible and corresponding the the right indices in your tableview. Also check that your tableView delegate and datasource are correctly set.
reloadData method as the following comment :
- (void)reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks
you may also have trouble reloading single rows if your table is mutating, then you should beginUpdate / endUpdate methods
- (void)beginUpdates; // allow multiple insert/delete of rows and sections to be animated simultaneously. Nestable
- (void)endUpdates; // only call insert/delete/reload calls or change the editing state inside an update block. otherwise things like row count, etc. may be invalid.

How To Set Nav Bar Item Title From plist's index row?

My data is sourced in a plist and displayed in a tableview with navigation bar.
I want to load the subview with the cell that was clicked.
I know this would set it correct, but I'm not sure how to implement indexPath in the viewDidLoad method?
self.navigationItem.title = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey: #"name"];
You have an array of Dictionary With you. Now IndexPath is seen called in TableView delegate methods. I doesn't mean that you can't create one. Create an NSIndexpath obect and assign the indexes parameters and send it to the function which contains the above code( separate the code which sets the title from the Cell for row at index path).
But the problem is as I see, You want to show only a specific set of data in dictionary which is stored in an Array. The real problem is You should know which the index of the Array you want to load. to the title and the tableview. If you can know that then its solved.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html
Regards ,
Jackson Sunny Rodrigues

How do I set a custom cell for non databound TableCells

I am happily able to set the styling of my tableviewcells using the cellForRowAtIndexPath delegate, but I want to set the background of all the nonpopulated cells which are still on the screen
hey, basically any cell which is part of your tableview belongs to a particular index and particular row
so incase you are not populating a particular section via data source - you can still get a reference to a cell in that indexPath by manually calling the cellForRowAtIndexPath delegate, and passing the section as well as row index
it returns you a cell. assign it to your private variable and edit it the way you want to.
Use the visibleCells method on your table view. Then do what you wish to the cells.
Alternately, you could call indexPathsForVisibleRows to get just the index paths. Then call cellForRowAtIndexPath: to get the cell corresponding to each index path.