开发者

iPhone SDK - Passing Data in an Array that is NOT shown in a UITableViewCell when that UITableViewCell is Selected

I have have searched all over for a tutorial or something that could show me how to pass data from an array that populates a cells cell.textlabel.text field that also has additional information in that node...

For simplicity sake I'm taking data I'm parsing from a Webservice that's producing an XML file. Which returns 开发者_如何学运维a "ID", "Name", "OtherID" field. Then populating a UITableViewController with the parsed XML essentially just the "Name" field to cell.textLabel.text. I have all of this working.

However, what I need to happen now is:

When a user does select a row I need the "ID" and "OtherID" field (that are not shown in the cell) to be set into variables, switch views, and set the new view's UILabels with the variables from the old view.

And this is where I'm at a loss...

Could someone point me in the direction of a tutorial or code example of how this code be done? Or if you have something similar share that with me?

Thank you so much for your time!


When you setup the UITableViewCell in tableView:cellForRowAtIndexPath:, you're using data from a certain index in the array of results, based on the index path of the cell.

The easiest way to do what you ask is to use the index path for the selected cell and perform the same calculation as above to find that certain index. Then you can fetch everything you need from the original array. That would look something like this:

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

A slightly more complex (but possibly more robust) method would be to subclass UITableViewCell to add properties or ivars to hold the data you will need; this could be as simple as a single property to hold the entire value from the original array. Then in the selection method, you can fetch everything you need from the cell itself. That would look something like this:

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

And then:

- (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 ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜