How to fill UITableView after Initialization - iphone

I read tutorial here
http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/
which shows how to fill a UITableView at initialization. I can't find tut to do so after initialization (for example when User has clicked on a button).
Any suggestion ?

You would just update the data source and reload the table view.
Following that tutorial, you would do it like so:
- (IBAction)myButtonPressed:(id)sender
{
self.arrayData = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"MacBook", #"MacBook Pro", #"iMac"];
[self.tblSimpleTable reloadData]
}
This will tell the UITableView to reload the data source and adjust the amount of sections, rows etc.
Note that you might prefer to use a NSMutableArray instead.
[self.mutableArrayData addObject:#"iMac"];

Related

Load Data depending on what TableView Cell was selected

I have a table view. I want my app to work like the "Contacts" app on the iPhone. When you select a person in the app, it goes to a view that has the users name and phone number. and it doesnt matter which cell is selected, it always goes to the same view, but with different data. How can i achieve the same thing? I want one view controller and i want each cell to load certain data onto the presented view controller! And i would like to use NSUserDefaults (if possible). Any Help would be great, Thank You!
P.S. i know this is kind of a broad question, but im unsure where to find an answer i have searched and searched. Thanks again!
I want one view controller and i want each cell to load certain data
onto the presented view controller!
No problem. User taps on cell, your table's delegate gets a -tableView:didSelectRowAtIndexPath: message. Let's say the table's delegate and data source are both the view controller that manages the table (because that's a pretty common setup). So the view controller takes a look at the index path for the cell, grabs the associated data from wherever it keeps its data, instantiates the detail view controller with the data connected to the tapped cell, and pushes it.
The part that you're probably missing is some sort of model that stores the data displayed in the table. The view controller for the table needs to know about the model because it's the data source for the table. It already needs to know where to find enough data to configure each cell in the table. If you use that same model to store the detail data, you'll be able to use it to configure the detail view controller.
Example: Let's say you have a table with just one section. A simple model for that table could be an array of dictionaries. When your table view controller needs to populate a cell, it uses the row of the index path as an index into the array to get a dictionary, and it uses that to set up the cell. Exactly the same thing happens when the user taps a cell: you use the row from the index path to get the right dictionary from the array, and you use that to set up the detail view controller. You could even just pass the whole dictionary to the detail view controller and let the detail view controller get what it needs. That way the table view controller doesn't have to worry about exactly which details the detail view controller displays.
You create an array of names (strings) and display those names in your table view.
Then once a name was selected you save it in NSUserDefaults as a string.
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
NSString *selectedString = [namesArray objectAtIndex:[indexPath row]];
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
[infoSaved setObject:selectedString forKey:#"theName"];
[infoSaved synchronize];
[self performSegueWithIdentifier:#"segueToMoreInfo" sender:self];
}
Then at your second view controller you load the information from an NSDictionary that contains NSDictionary items.
-(void) loadData {
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
NSString *theName = [infoSaved objectForKey:#"theName"];
NSDictionary *currentNameToDisplay = [itemsDictionary objectForKey:theName];
// then you can set your labels with information in that NSDictionary item
[nameLabel setString:[currentNameToDisplay objectForKey:#"userName"];
[ageLabel setString:[currentNameToDisplay objectForKey:#"userAge];
}
That would be a simple basic structure.
You can save the main Names NSDictionary that contains the items into a plist file or even better save the dictionary in NSUserDefaults.
Hope that helps.

constant tableviewcell at the beginning of the tableview

I would like to know if there is any sneaky way of getting a UITableViewCell to appear at the beginning of a UITableView no matter what the array that is going to populate the tableview contains?
I am having issues where I would like to have a "select all" cell at the top of the tableview but currently having issues trying to adjust the arrays I have going into the tableView:cellForRowAtIndexPath:
Not sure of your end goal, but there are many ways some easier (sneaky ways) than others depending on your needs. It sounds like your items are fairly static, so you can just insert them before you display or update the table row.
say you have a datasource called self.items and you did something to get data. Maybe in your app you are round tripping to some data source based upon input, like sort selectors or from the search bar delegate.
try something like this in the area where you load your datasource.
NSString *myCustomObject = #"Jump To Songs";
self.items = [self getGetMovieList];
[self.items insertObject:myCustomObject atIndex:0];
[self.tableView reloadData];
Another easy way would be to add sections to your table and just make the first section your navigation items.
Note: you may need to handle the actions in didSelectRowAtIndexPath......
there you go, not so sneaky, but pretty simple.
be well
You can make two types of UITableViewCell, then return the "select all" one if indexPath.row == 0.
On the other hand, how about just make a UIView for the "select all" functionality, and set it as the tableHeaderView of your table view?

Paging in uitableview

I am using a UITableview.In that there are nearly 100 records.I want to show 10 records at a time after that i will click on a button or UIPageControl which will show the next 10 records.So,please suggest me how can i do it?
If you just wanna create a table like that you could follow the below outlines.
In your button click Append or change the data in your data source array of your tableview. (Depending upon your requirement)
[self.myDataSourceArray addObjects:#"obj", #"obj2", nil];
Reload the table
[self.tableView reloadData];
UPDATE FOR THE COMMENT:
OK let me add some code,
//Consider this method is called when you click the next button
- (void)nextButtonAction
{
//Remove the older objects
[self.myDataSourceArray removeAllObjects];
//Add your new objects to the array
[self.myDataSourceArray addObjects:#"obj", #"obj2", nil];
//Reload the table
[self.tableView reloadData];
}
You could have a forward and backward button in the navigation bar with the page number
Define a rect would you like to scroll and then call method scrollRectToVisible:newRect for UITableView
CGRect newRect = CGRectMake(0, 0, tableView.frame.size.width, tableViewCell.height*10);
[tableView scrollRectToVisible:newRect animated:YES];
I am not sure if it is a good thing to introduce a pagination mechanism in your case. Because a UITableView should be fine for handling around 100 records. Just think of your Addressbook which can easily contain more than 100 records.
If you have a big number of records in a UITableView filtering and the index on the right with the letters of the Alphabet can help you to quickly navigate to the desired entry.
However, if you do decide to add more records to one table a common practize is to add a "Load more entries" cell at the very bottom of the table view.
For Example: Check out the Top50 Apps in the AppStore-App.
This really goes against Apple's User Interface Guidelines. TableViews are designed with built in highly efficient pagination (reusable cells). I have TableViews with 4000 records and when the data model behind the TV is done right, it can be very responsive.
If you are dead set on this, you could use a button on the NavBar, Toolbar, etc and use that to call scrollToRowAtIndexPath.
you can keep a count of the number of times the button is pressed
int i=0;
-(void)buttonPressed{
i=i+10;
[table reloadData];
}
then in
cellForRowAtIndexPath
{
cell.textLabel.text = [NSString stringWithFormat:#"%#",[Arr objectAtIndex:i+indexPath.row]];
}

Updating the UISearchBar data source from Core Data in the right place

In my app, I have a search bar above my table view. My problem is this:
If I go into my table view and add an item to any level of the hierarchy, and then go back to the root (where the search bar is) and try to search for it, it doesn't show up in the results it.
For example, if I already have itemOne and itemTwo in core data when the app is launched, I can search and find them just fine. But if I add itemThree and try to search for it, it is not found.
The array that my search bar uses to load all of the items from Core Data into is located in the viewDidLoad method:
- (void)viewDidLoad
{
listOfItems = [[NSMutableArray alloc] init];
NSMutableArray* allItemArray = [CoreDataHelper searchObjectsInContext:#"Item" :entitySearchPredicate :#"Description" :YES :managedObjectContext];
NSDictionary *allItemDict = [NSDictionary dictionaryWithObject:allItemArray forKey:#"Items"];
[listOfItems addObject:allItemDict];
}
I know that the viewDidLoad isn't running after I add the last item, and that is my problem. But I don't know where to put this code that it will update with any items that have been added.
Any input is appreciated. Thanks.
Do the same things on viewWillAppear ..things will be right.

Preload cells in tableView for use with mapView

I have a mapView and tableView, with each annotation in the mapView corresponding with a cell in the table. What I want to do is select the appropriate cell anytime an annotation is selected on the map.
As of right now, im creating an NSDictionary when the cells are created, which maps the row number to the annotationID. This works, but the problem is that the dictionary isnt completely populated until all the cells have been created, and all the cells arent created until youve scrolled all the way through the table. Thus, when the app starts for the first time, only the 4 annotations originally visible can be selected from the mapView.
So what im looking for is either a method to automatically populate my dictionary, or a better way of accomplishing what i need to do. Thanks a lot!
Well, first thoughts are that instead of populating the NSDictionary during scrolling, just populate it during viewDidLoad ..I do something similar where I populate all of my data into an NSDictionary and then use that to initialize/update the UI on the cells during scrolling for re-usable cells.
Also, by putting your data into an array of some kind, you can also map it to the cells. Just remember that arrays start at 0, so position in table = indexOfYourArray + 1
Simple example of loading an array stored in a plist into an NSArray in viewDidLoad
// Load the data
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:someArrayNameString ofType:#"plist"];
self.someArrayYouCreated = [NSArray arrayWithContentsOfFile:pathToFile];
Now that you just dumped a whole bunch of data into that array, you can populate it during scrolling in cellForRowAtIndexPath
NSDictionary *dataItem = [someArrayYouCreated objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.textColor = [UIColor colorWithRed:(58/255.f) green:(58/255.f) blue:(58/255.f) alpha:1.0];
label.text = [dataItem objectForKey:#"PersonName"];
With this example, you are just populating the cells from an array that you created in viewDidLoad and therefore all of your data is ready to use almost right away. If you have a ton of data, you could also throw up a progress circle to delay for a second until the array is finished loading.
Again, I don't quite understand how you are storing your data (since you have not said anything about that) so I can only speculate that this will work for you. Until you provide more detail, this is the best I can do.