在 UI 表行中设置唯一标识符的方法,可在选择时检索
因此,我有一个 MyClass 类型的对象数组,并定义了以下变量:
@property (retain) IBOutlet NSString* productName;
@property (retain) IBOutlet NSString* brandName;
@property (retain) IBOutlet NSString* category;
@property (retain) IBOutlet NSString* productDetails;
@property (retain) IBOutlet NSString* productPrice;
@property (retain) IBOutlet NSString* points;
@property (retain) IBOutlet NSString* image;
并且我设置了构造函数,并正确设置了 MyClass 类的所有内容。在我的一个视图控制器中,我正在创建 MyClass 的对象,并且我可以使用 Product.productName = @"SomeProduct" 等来正确设置和获取。
现在这就是我被困住的地方。我将 TableViewController 嵌入到我的视图之一中,该视图将 ProductName 显示为每个单元格的 textLabel 属性。用户可以通过滚动表格来选择产品。但是,当我选择一行时,我需要一种方法来访问单元格源自的对象,以便填充屏幕右侧的其他信息(图像和其他产品信息)。有没有办法可以设置一个隐藏的唯一 ID 来轻松识别该对象?我真的不想根据与product.productName 匹配的文本标签来选择对象,并且我认为当我扩展应用程序时这不会起作用。如果有一种方法可以将对象本身“存储”到单元格中,那就太好了。如果没有,在单元格中存储密钥的某种方式可以让我构建一个哈希表来获取我需要的信息。
So I have an array of objects of type MyClass, and the following variables are defined:
@property (retain) IBOutlet NSString* productName;
@property (retain) IBOutlet NSString* brandName;
@property (retain) IBOutlet NSString* category;
@property (retain) IBOutlet NSString* productDetails;
@property (retain) IBOutlet NSString* productPrice;
@property (retain) IBOutlet NSString* points;
@property (retain) IBOutlet NSString* image;
And ive setup the constructor and everything properly wrt the class MyClass. In one of my view controllers, I'm creating objects of MyClass, and i can use the product.productName = @"SomeProduct" etc. to set and get properly as well.
Now here's where I'm stuck. I'm embedding a TableViewController in one of my views which displays the productName as the textLabel property of each Cell. Users can choose between the products by scrolling through the table. However, when I select a row, I need a way to access the object the cell originated from in order to populate other information on the right side of the screen (image, and other product info). Is there a way i can set a hidden Unique ID which would easily identify the object? I really don't want to select the object based on the textlabel matching product.productName, and I don't think that would work when I scale the application up. If there's a way to "store" the object itself into the cell, that would be great. If not, some way of storing a key in the cell would let me build a hash table to get the info i need..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用以下方法在每个 UITableView 单元格上设置文本标签。在此方法中,您找到适当的 MyClass 对象并将 UITableView 单元格的 textLabel 属性设置为 MyClass 对象的属性。
只要您当前使用此方法来填充单元格,您基本上就可以将用于查找正确的 MyClass 对象的代码复制到以下方法中,该方法在选择单元格时激活:
而不是在找到该对象后设置属性正确的 MyClass 对象,调用方法以使用正确的信息更新屏幕右侧的视图。
查看 UITableView 类参考。
You should be using the following method to set the text label on each of your UITableView cells. In this method you find the appropriate MyClass object and set the textLabel property of the UITableView cell to a property of the MyClass object.
As long as you are currently using this method to populate your cells you can basically copy the code you use to find the correct MyClass object into the following method, which is activated when a cell is selected:
Instead of setting a property once you find the correct MyClass object, call a method to update the view on the right side of the screen with the correct information.
Have a look at the UITableView class reference.