Confused_person
Confused_person

Reputation: 103

How to make a cell editable

I am currently trying to give user the ability to edit the content of the multiple cells. Once updated, the data will be sent to web service.

Ok right now, as far as I have read, there is only "delete" and "add" row(s). I can't seem to find any guides or tutorials on how to edit the content of cell(s).

Your suggestion(s) and/or advices are much appreciated.

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   

Upvotes: 1

Views: 221

Answers (1)

Nishant Tyagi
Nishant Tyagi

Reputation: 9913

You can not edit cell content directly. If you need to edit content then add UITextField or UITextView in cell as its subview. and then access them.

EDIT : You can add textfield as below :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        // ADD YOUR TEXTFIELD HERE
        UITextField *yourTf = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, 330.0f, 30)];
        [yourTf setBackgroundColor:[UIColor clearColor]];
        yourTf.tag = 1;
        yourTf.font = [UIFont fontWithName:@"Helvetica" size:15];
        yourTf.textColor = [UIColor colorWithRed:61.0f/255.0f green:61.0f/255.0f blue:61.0f/255.0f alpha:1.0f];
        yourTf.delegate = self;
        [cell addSubview:yourTf];

    }

    // ACCESS YOUR TEXTFIELD BY REUSING IT
    [(UITextField *)[cell viewWithTag:1] setText:@"YOUR TEXT"];

    return cell;
}

Implement UITextField delegates and then you can edit cell content using this UITextField.

Hope it helps you.

Upvotes: 3

Related Questions