How to get indexPath from one class to another

懵懂的女人 提交于 2020-01-06 15:15:58

问题


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:


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.



来源:https://stackoverflow.com/questions/3185760/how-to-get-indexpath-from-one-class-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!