如何从一个类到另一个类获取indexPath

发布于 2024-09-08 07:51:50 字数 183 浏览 1 评论 0原文

我有一个应用程序,其中我的主要视图是表格视图。其中每个单元格加载另一个包含某些数据的视图。现在,每个单元格都不同,因此每个单元格加载的视图中的数据应该不同。为此我需要indexPath。我为 TableView 创建了一个类,为简单的 UIView 创建了另一个类。如何将 indexPath 值从一个类转发到另一个类。

提前致谢 尼克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2024-09-15 07:51:50

如果我明白你在问什么,你有一个 TableView (TableView1),其中每个单元格加载一个新视图 (MyView1),其内容取决于单元格的 indexPath。当您收到 isSelected 方法时,您不能将 indexPath 传递给您正在创建的新视图的构造函数吗?例如,

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

您将需要创建自己的类 MyView1,它继承自 UIView,并添加构造函数以接受额外的参数并存储它。

如果您没有创建 UIView 或者它已经存在,那么我不完全确定您在问什么,所以我只是猜测一下。如果您询问如何在 UIView 之间共享信息,并且您的问题是每个有问题的 UIView 都不了解对方。

在全球范围内共享有关应用程序的信息的一个简单方法是创建一个遵循 Singleton 设计模式的静态类,每个 UIView 都可以访问和修改该静态类。

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

在源(.m)文件中,

//source
static MySingleton* singleton = nil;

@implementation MySingleton 
@synthesize myIndexPath;

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

我可能误解了您的问题,并且此解决方案可能不是解决您的问题的最佳方法。请澄清您的界面设计,我们可以提供更多帮助。祝你好运。

--编辑:
根据你的说法,第一个解决方案应该适用。
当您的 TableView 收到“isSelected”消息时,您应该创建 WebView 对象。只需覆盖 init 函数即可接受您需要的任何参数,在本例中为所选表的 NSIndexPath。我确信在developer.apple.com 上有一些使用自定义UITableView 和导航视图控制器搜索示例项目的示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文