Ios4 iPhone SDK-在选定UITableViewCell时,在UITableViewCell中未显示的数组中传递数据
我已经搜索了所有的教程或其他东西,可以告诉我如何从填充cells cell.textlabel.text字段的数组中传递数据,该字段在该节点中还包含其他信息 为了简单起见,我从生成XML文件的Web服务中获取解析的数据。它返回一个“ID”、“Name”、“OtherID”字段。然后用解析后的XML填充UITableViewController,基本上就是cell.textLabel.text的“Name”字段。所有这些我都能用 然而,我现在需要做的是: 当用户选择一行时,我需要将“ID”和“OtherID”字段(未显示在单元格中)设置为变量,切换视图,并使用旧视图中的变量设置新视图的UILabel 这就是我迷茫的地方 有没有人能给我指点一下如何编写这段代码的教程或代码示例?或者如果你有类似的东西和我分享Ios4 iPhone SDK-在选定UITableViewCell时,在UITableViewCell中未显示的数组中传递数据,ios4,uitableview,didselectrowatindexpath,Ios4,Uitableview,Didselectrowatindexpath,我已经搜索了所有的教程或其他东西,可以告诉我如何从填充cells cell.textlabel.text字段的数组中传递数据,该字段在该节点中还包含其他信息 为了简单起见,我从生成XML文件的Web服务中获取解析的数据。它返回一个“ID”、“Name”、“OtherID”字段。然后用解析后的XML填充UITableViewController,基本上就是cell.textLabel.text的“Name”字段。所有这些我都能用 然而,我现在需要做的是: 当用户选择一行时,我需要将“ID”和“Ot
非常感谢您抽出时间 在
tableView:cellforrowatinexpath:
中设置UITableViewCell时,使用的是结果数组中某个索引的数据,该索引基于单元格的索引路径
执行所需操作的最简单方法是使用所选单元格的索引路径,并执行与上面相同的计算以查找特定索引。然后可以从原始数组中获取所需的所有内容。看起来是这样的:
- (NSUInteger)arrayIndexFromIndexPath:(NSIndexPath *)path {
// You could inline this, if you like.
return path.row;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path {
NSUInteger index = [self arrayIndexFromIndexPath:path];
NSDictionary *data = [self.dataArray objectAtIndex:index];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease];
}
// ... Set up cell ...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
NSUInteger index = [self arrayIndexFromIndexPath:path];
NSDictionary *data = [self.dataArray objectAtIndex:index];
// ... Do whatever you need with the data ...
}
一个稍微复杂(但可能更健壮)的方法是将UITableViewCell子类化,以添加属性或IVAR来保存所需的数据;这可能很简单,只需一个属性就可以保存原始数组中的整个值。然后在选择方法中,您可以从单元格本身获取所需的一切。看起来是这样的:
- (NSUInteger)arrayIndexFromIndexPath:(NSIndexPath *)path {
// You could inline this, if you like.
return path.row;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path {
NSUInteger index = [self arrayIndexFromIndexPath:path];
NSDictionary *data = [self.dataArray objectAtIndex:index];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease];
}
// ... Set up cell ...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
NSUInteger index = [self arrayIndexFromIndexPath:path];
NSDictionary *data = [self.dataArray objectAtIndex:index];
// ... Do whatever you need with the data ...
}
MyTableViewCell.h:
@interface MyTableViewCell : UITableViewCell {
NSDictionary *data_;
}
@property (nonatomic, retain) NSDictionary *data;
@end
MyTableViewCell.m:
@implementation MyTableViewCell
@synthesize data=data_;
- (void)dealloc {
[data_ release]; data_ = nil;
[super dealloc];
}
@end
然后:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path {
NSUInteger index = [self arrayIndexFromIndexPath:path];
NSDictionary *data = [self.dataArray objectAtIndex:index];
// Remember, of course, to use a different reuseIdentifier for each different kind of cell.
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...];
if (!cell) {
cell = [[[MyTableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease];
}
cell.data = data;
// ... Set up cell ...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
MyTableViewCell *cell = [tableView cellForRowAtIndexPath:path];
NSDictionary *data = cell.data;
// ... Do whatever you need with the data ...
}
在视图控制器之间共享数据的最佳方式是什么?