如何从一个类到另一个类获取indexPath [英] How to get indexPath from one class to another

查看:85
本文介绍了如何从一个类到另一个类获取indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中我的主视图是表格视图.每个单元加载另一个包含某些数据的视图.现在,每个单元格都不同,因此每个单元格加载的视图中的数据也应该不同.为此,我需要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.

先谢谢了 尼克

推荐答案

如果我理解您要问的内容,那么您就有一个TableView(TableView1),其中每个单元格加载一个新视图(MyView1),其内容取决于indexPath的细胞.当您收到isSelected方法时,是否不能仅将indexPath传递给您要创建的新视图的构造函数?例如

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
}

然后,您需要创建自己的类MyView1,该类从UIView继承,并添加构造函数以接受额外的参数并将其存储.

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.

如果您没有创建UIView或它已经存在,那么我不确定您要问的是什么,因此我只作一个猜测.如果您问如何在UIView之间共享信息,那么您的问题是每个有问题的UIView彼此都不了解.

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.

一种简单的全局共享有关您的应用程序信息的方法是按照每个UIView可以访问和修改的Singleton设计模式创建一个静态类.

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

在源(.m)文件中

//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.

- 用你的话说,第一个解决方案应该适用. 当您将'isSelected'消息发送到TableView时,您应该正在创建WebView对象.只需覆盖init函数即可接受所需的任何参数,在这种情况下为所选表的NSIndexPath.我敢肯定,developer.apple.com上有一些示例,它们使用自定义的UITableView和Navigation View Controller搜索示例项目.

-- 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.

这篇关于如何从一个类到另一个类获取indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆