views:

29

answers:

1

I have an app where my primary view is a tableview. where each cell loads another view containing certain data. Now, each cell is different so the data in the view loaded by each cell should be different. For that I need the indexPath. I've created a class for the TableView and another class for a simple UIView. How do I forward the indexPath value from one class to the other.

Thanks in advance Nik

+1  A: 

If I understand what you are asking you have a TableView (TableView1) in which each cell loads up a new view (MyView1) with content dependent on the indexPath of the cell. When you receive the isSelected method, can't you just pass the indexPath to the constructor of the new view you are creating? For example

–(void) selectRowAtIndexPath:(NSIndexPath*) indexPath animated:(BOOL) animated (UITableViewScrollPosition)scrollPosition {
 UIView* view = [[[MyView1 alloc] initWithIndexPath:indexPath] autorelease];
 //push or load the view
}

You will then need to create your own class MyView1 which inherits from UIView, and add the constructor to accept the extra parameter and store it.

If your not creating the UIView or it already exists, then I'm not completely sure what you are asking, so I'll just have a guess. If you are asking how to share information between to UIView's, and your problem is that each UIView in question does not know about the other.

A simple way to share information globally about your app would be to create a static class following the Singleton design pattern which each UIView can access, and modify.

//header
@interface MySingleton {
   NSIndexPath* myIndexPath;
}
@property (nonatomic, retain) myIndexPath;
+(id) globalSingleton;
@end

In the source (.m) file

//source
static MySingleton* singleton = nil;

@implementation MySingleton 
@synthesize myIndexPath;

+(id) globalSingleton {
   if (singleton == nil) {
      singleton = [MySingleton new];
   }
   return singleton;
}
@end

Its possible I've misunderstood your question and this solution might not be the best way to solve your problem. Please clarify the design of your interface and we can help more. Good Luck.

--EDIT: From what you say, the first solution should be applicable. When you get the 'isSelected' message to your TableView, you should be creating the WebView object. Simply overwrite the init function to accept whatever parameters you need, in this case the NSIndexPath of the selected table. I'm sure there are some examples at developer.apple.com search for the example projects using customised UITableView's and Navigation View Controllers.

Akusete
ok. im trying to create a library where i have table view with all possible items in library and each library item moves to another view containing details of that item. I've created a class for the TableView and a separate class for the DetailView which is a normal UIWebView. The problem is, that for handling data in table i can use indexpath variable provided in cellForRowAtIndexPath. But, i need the indexpath to handle data in the detail view too. so that each cell clicked in the tableview loads only the data relevant to that cell. kinda like a switch case.
Nick
P.S. I'm newbie.so my apologies if im not able to explain as well.
Nick
I understand your solution,but my UIWebView is part of a navigation controller. So, I'm initializing it with a nib file using initWithNibName.
Nick