views:

32

answers:

2

Hey everybody, I had a question about editing the value of a table cell and returning the edited value back to the original cell.

I have a UITableView which contains 5 sections of 1 row each. Within cellForRowAtIndexPath I have the following:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }   

    NSString *fieldTitle;
    switch (indexPath.section)
    {
        case 0:
            fieldTitle = @"First Name";
            break;
        case 1:
            fieldTitle = @"Last Name";
            break;
        case 2:
            fieldTitle = @"Company";
            break;
        case 3:
            fieldTitle = @"Email Address";
            break;
        case 4:

                fieldTitle = @"Password";
                break;
    }

cell.textLabel.text = fieldTitle;

When a row is clicked, didSelectRowAtIndexPath is fired as follows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    AddField *addField = [[AddField alloc] initWithStyle:UITableViewStyleGrouped];

    addField.field = cell.textLabel.text;
    addField.title = cell.textLabel.text;
    addField.value = cell.detailTextLabel.text;

    [self.navigationController pushViewController:addField animated:YES];

}

This sets up another UITableView that contains one section and one row. This table utilizes a custom cell I wrote that contains a text field the user can edit. There is a done button the user can click to go back to the previous view.

My question is this: How do I get the value the user entered in the AddField view to appear in the detailText label on the selected table cell in the previous view? This functionality would be very similar to adding the title of a new event in the iPhone's native calendar application.

Thanks for any help I can get, and let me know if you need more information.

A: 

Have you tried calling reloadData on the original tableView? Of course you would need chnage your code so that fieldTitle to get its values from an array of NSStrings......and set the tableview datasource to that array....

ennuikiller
A: 

You would use delegates for this. Declare a protocol which is your cell delegate as follows (from memory so apologies if it doesn't compile):

@protocol CellDelegate

@required

- (void) fieldValueChanged:(AddField *)field;

@end

Add an "id delegate" to your custom cell class, and add a property (nonatomic, assign) and synthesise it.

Then when you setup your cell in didSelectRowAtIndexPath you do the following:

[cell setDelegate:self];

Now in you view controller you simply implement that protocol (CellDelegate), implement the method fieldValueChanged.... this will get called by the cell.

Now in the cell when the value is changed simply call [delegate fieldValueChanged:addfield];

NOTE: really you should be setting up the AddField WITHIN the custom cell class you write...

Simon Lee