Reveclair
Reveclair

Reputation: 2421

How to edit the text content of a row in a tableview?

I'm using Xcode 6.

Let's say I have a Food object displayed in a tableview (each row being a property : name, calories, protein, vitamin C, ...). I would like to make each row content editable.

At the beginning, I was thinking of, after clicking on a "Edit" button, making the rows content as textfields but this solution seems to complicated.

Now, I think of, after selecting one row, pushing a new table view controller with just one row containing a label (the name of the property, for example "calories") and a textfield containing the value. (In fact, I was inspired by the interface of the Settings in iPhone / iPod, General > About > Name)

1- Can I make this table view controller with just one row reusable for the various properties of my Food object (name, protein, vitamin C, ....) by passing it the property name and the value depending on which row is selected ?

2 - if yes, is this a clean way to achieve what I want to do ? Do you have other ideas ?

PS : I should precise that my values can be string (like the name property) or numbers (protein, vitamin C, ...).

Thank you very much

enter image description here

Upvotes: 1

Views: 1445

Answers (2)

tobinjim
tobinjim

Reputation: 1852

I think your solution is best handled through the UITableViewCell class, which has control methods for starting the editing of cell contents. A Closer Look at Table View Cells

Upvotes: 1

EnriMR
EnriMR

Reputation: 3970

I'm going to show you what I have done in one of my apps. We have a profile section waht is a tableViewController with a lot of cells for user profile data. And if a user want to edit that values, he or she has to click on the cell and we will show him/her an alert with an input text to specify the new value.

For example, if you want to edit your description in our app:

enter image description here

enter image description here

So you need to implement these.

Some variables to store what cell you want to edit

@interface MyViewController (){

    //...

    NSString *actionSheetType; // to store what alert I have to show

    NSString *description; // In my example, to store my user desription

    //...
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath to catch the cell clicked

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        //...

    } else if (indexPath.row == 2) {
        //...

    } else if (indexPath.row == 3) {
        //...

    } else if (indexPath.row == 4) {

        // Description

        actionSheetType = @"description"; // Set the type

        // Show the alert
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Edit description", nil) message:[[NSUserDefaults standardUserDefaults] stringForKey:USER_DESCRIPTION] delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
    } 

}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex to get the new value

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Check what alert have you shown
    if ([actionSheetType isEqualToString:@"password"]){

    } else if ([actionSheetType isEqualToString:@"location"]){

    } else if ([actionSheetType isEqualToString:@"description"]){
        if (buttonIndex == 1) // if click on my alert button
        {
            // Edit my description
            description = [alertView textFieldAtIndex:0].text;
            // Any more actions you will need to do. For example, if you modify a cell or a table alement, reload the table
        }
    } else if ([actionSheetType isEqualToString:@"email"]){
        if (buttonIndex == 1)
        {
        }
    }
}

I hope this can help you! If you have any doubt, please leave a comment and I will answer you as soon as possible :)

Upvotes: 3

Related Questions