reload TableView with data - iphone

Basically i have a view with just a table view on it and on another tab you can add to this table view. but when you click the tab with the table view in it isnt updating it self, so how would i reload the data in the table view i have tried :
- (void) viewWillAppear: (BOOL) animated
{
[ThirdViewController.tableView reloadData];
}
with no success as i get error Expected ':' before '.' token.
all of the cells are added via
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Any help would be great

Looks like ThirdViewController is the name of a class, not an object.
Try just
[tableView reloadData];

You can modify your app in the following way
1 Take a segmentation control above the table view.
2 When the user taps on any of the segment,take separate View Controller which is pushed onto the stack.
Cheers

Related

Troubles with datasource and tableviews

I've got an UIViewController with 2 tableviews:
1- the main tableview, which is shown in the whole view controller
2- the second viewcontroller, which is loaded in a popup view.
The second tableview is shown on swiping a cell of main tableview.
Depending on which cell is swiped, there are different data in popup view.
I've already loaded the whole data in viewdidload method and stored everything in nsmutablearray, so are ready to be loaded.
My problem is that I don't know how to work with tableview's DataSource, in my project i linked both tableview's datasource to file's owner, but in this way it loads the numberofrows from the main view, and it doesn't take the correct count which should have the second tableview.
So, if in main tableview i have for example 3 elements, and in the second tableview it should load 5 elements, it gives me an error, ('NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index…).
I think that i should link my tableviews to different datasource, even if i really don't know.
I hope to be clear, if you need more info, or a better description, ask me and i'll do it for you.
In the delegate method, you should compare the tableview.
See the example,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == maintableView)
return VALUE_FOR_MAIN_TABLE_DATA;
else
return VALUE_FOR_POP_TABLE_DATA;
}
No Problem
you just set tags to your tables.
[tableView1 setTag:1];
[tableView2 setTag:2];
and then
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView.tag == 1) {
return 4;
}
else if (tableView.tag == 2)
{
return 5;
}
}
do similar thing all data source method
You can use one View Controller as an unique data source for multiple table view, but you'll need to check which table view is requesting data using the tableView arguments of the UITableViewDataSource methods.
For example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTableView)
{
// code for your main table view
}
else
{
// code for your popup table view
}
}
The same instance of a view controller serving as data source/delegate for two tables is technically possible but has a number of traps. One of these is: to which of the two tables does self.view refer?
However, most of the data source protocols hand down a reference to the table. You could use that rerference to determine which table acutally sends the request. But not all methods of the protocol inclulde a reference to the table. And that is where it starts getting tricky.
You are far better off with two distinctive view controllers, one for each table. Establish a protocol between them so that the main view controller can hand down the data to the one in the popup window so that the popup can initialize/load it self with the proper data and can refresh its view when ever the data changes.
You will need to check for the tableview in all your delegate and datasource methods as follows:
if (tableView == mainTable)
{
// code for your main table
}
else if (tableView == popupTable)
{
// code for your popup table
}
You do same for 2 or more table views.
Hope this helps.
You can do it both ways:
Make separate classes as data source for separate tables. Instantiate their objects as datasources for tables and bind them at viewDidLoad method in proper view controller.
Make one datasource for 2 tables which I don't recommend as it is not comply with proper OOAD. You'll have tight coupling this way between view controller and the table which can be cause of trouble in the near future.
You have method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
in which you can distinguish to which table you are referring:
if (tableView == mainTableView) {
// initialize cell for main table view
}
if (tableView == secondTableView) {
// intialize cell for second table view
}

How to send particular row data to previous view on select of row in current tableview?

I have a UIButton in the first view. When I click on a button I pushed a 2nd view having table view with data .i want the data of particular row selected by the user in 2nd view sent to the first view.In short second view row data on 1st view while selecting a row on poping to first view?please help .
thanks in advance.
You can use delegate to send data from selected row in 2nd view to the 1st view.
I wrote a tutorial on how to use delegates here: http://krodev.wordpress.com/2012/10/08/objective-c-delegates/.
When you setup a delegate, use 2nd view method to send selected row's data back to 1st view:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// row index
int rowIndex = indexPath.row.
// when you have row index you can get row data from your table view datasource
// if your data is NSString (method name depends on your implementation of delegate)
[self.firstViewDelegate selectedRowsData:(NSString *)data];
// return to the 1st view
[self.navigationController popToRootViewControllerAnimated:YES];
}
Go to your second view, In the method of your UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Add the condition if(indexPath.row == 0) in which you would use a global string already declared in your AppDelegate.m file to pass data to your previous view using the popViewController method.
This string will be equal to your cell.textLabel.text.
Example
if(indexPath.row == 0)
{
textString = cell.textLabel.text;
}
and so on for your various rows. Similarly you can add the textString to your cell of the first table in the 1'st view. Any doubt please tell. Thanks :)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Your First View Controller.index = index path.row
}

Adding Navigation Effect to tableView reloading

I am working on
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!XAppDelegate.isRegular)
{
if([self shouldReloadWithSubCategory:[NSString stringWithFormat:#"%#",[[categoriesArray objectAtIndex:indexPath.row]objectForKey:#"CategoryId"]]])
{
[KeysArray addObject:categoriesArray];
categoriesArray = [categoriesContainer objectForKey:key];
NSLog(#"%#",categoriesArray);
[tv reloadData];
}
} }
I have a category and sub category. when select on a category, sub category is appeared on same view controller by reloading tableView. I want to see navigation effect when we push a view controller to navigationControoler, but without using another view controller. How can I do this?
There are much more code for this implementation. Too easy create another DetailTableViewController.
But if you want to realize using you rules:
1) I must create any flag that means Common view OR Detail view
2) Create containers for categories and subcategory.
3) Dependency flag load different resource in data source.
4) Reload table view
To get navigation effect, try to push self. If it not resolve you problem - create new object with correct fields.

How to display a list of item in tableview when the table did select row at index method is called?

I have an application in which I have tableview in which I return 3 rows in the tableview. When I select a particular row in the table view another tableview should open which should display list of items and when I select a particular item the value of that particular item should be set to the textlabel of previous tableview.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView != secondTableview) {
self.secondTableviewArray =[[NSMutableArray alloc]initWithObjects:#"secondfirst",#"secondSecond",nil];
[secondTableview reloadData];
}
}
Pass the second controller (the one that will appear) a weak reference to the first just before showing it. Once the second view controller has appeared and the user has made the selection (you can catch that in (-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;), send a message to the first giving the value selected.
The first view controller, then can store the new value, and even dismiss/pop the second.
This can also be achieved, by writing your own delegate protocol, so the first conforms to it, and is the delegate for the second.
Take NSString in second view and use the property.from the first view to second view set the string and in second view use that String
If you want to open new table view on click of row then you make new xib file and call like as define in below code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XMLBooksViewController * xmlBooksViewController = [[XMLBooksViewController alloc] initWithNibName:#"DetailView" bundle:nil];
XMLBooksAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
xmlBooksViewController.author = [appDelegate.authors objectAtIndex:indexPath.row];
[self.navigationController pushViewController:xmlBooksViewController animated:YES];
[xmlBooksViewController release];
}
In next file you define another table view, so the list or items comes as table view.

UITableCell - How do I turn off the selected state of the cell when user navigates back to view?

I think this is a very common situation, but I can't find how everybody else solves the problem.
I have a UITableView, and when a user taps a cell I push a new view controller (using UINavigationController) on the stack. When the user taps "Back" on the navigation bar, the cell still appears in selected state (i.e. blue background).
I want the background to be blue initially, when the user tapped the cell, but to be turned off when the page is shown again.
you could deselect the cell before or after you have pushed the new viewcontroller.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
// create and push new viewController
}
There are two ways you can do the same thing:
Either Reload the whole Table in View will Appear
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[YourTableView reloadData];
}
OR Either
-(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[YourTableView deselectRowAtIndexPath:indexPath animated:YES];
}
Looking at performance wise second one is best solution to use.