Krishna
Krishna

Reputation: 241

enable/disable edit mode in UITableViewCell

I have a custom UITableViewCell that has 1 textfield and a label on it. I would like to enable the textfield editing only when the tableview is in edit mode.

I'm using following methods to enable textfield edit mode and disable textfield edit mode. but this is not working. I'm not sure whether this is correct approach or not. If it's not the correct way, can you let me know how to disable enable textfield?

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *rowToSelect = indexPath;
    EditingTableViewCell *detSelCell;
    detSelCell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    detSelCell.textField.enabled = self.editing;

    // Only allow selection if editing.
    if (self.editing) 
    {
        return indexPath;
    }
    else 
    {
        return nil;
    } 
}

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

        if (!self.editing) 
        {
            return;
        }
        EditingTableViewCell *detcell;
        detcell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            detcell.selectionStyle = style;
            detcell.textField.enabled = self.editing;

also I've the following lines:

self.tableView.allowsSelection = NO; // Keeps cells from being selectable while not editing. No more blue flash.
self.tableView.allowsSelectionDuringEditing = YES; // Allows cells to be selectable during edit mode.

Please help!

Upvotes: 2

Views: 5276

Answers (3)

Bluedays
Bluedays

Reputation: 151

Since I've been looking for the same for some time, here's the full code achieving it

TextFieldTableViewCell.h

@interface TextFieldTableViewCell : UITableViewCell <UITextFieldDelegate> {
        UITextField *textField;
    }

    @property (nonatomic, strong) UITextField *textField;
    // add delegate/protocol to inform of change

@end

TextFieldTableViewCell.m

#import "TextFieldTableViewCell.h"

@implementation TextFieldTableViewCell

@synthesize textField;

- (void)initializeTextField {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.textField = [[UITextField alloc] initWithFrame:CGRectZero];
    self.textField.autocorrectionType = UITextAutocorrectionTypeDefault;
    self.textField.enabled = NO; // not editable unless the table is in edit mode
    // do all the necessary textfield setup here, font/alignment and so on

    [self addSubview:self.textField];

    self.accessoryType = UITableViewCellAccessoryNone;
}

// from code
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if( (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        [self initializeTextField];
    }
    return self;
}

// from storyboard
- (id)initWithCoder:(NSCoder *)aDecoder {

    if( (self = [super initWithCoder:aDecoder])) {
        [self initializeTextField];
    }
    return self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    // Extra code to be added here to notify a delegate that text has changed since cell is holding the value temporarily 
    // ..... 
    UITableView *tableView = (UITableView *)self.superview;
    [tableView deselectRowAtIndexPath:[tableView indexPathForCell:self] animated:YES];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect editFrame = CGRectInset(self.contentView.frame, 10, 10);

    if (self.textLabel.text && [self.textLabel.text length] != 0) {
        CGSize textSize = [self.textLabel sizeThatFits:CGSizeZero];
        editFrame.origin.x += textSize.width + 10;
        editFrame.size.width -= textSize.width + 10;
        self.textField.textAlignment = UITextAlignmentRight;
    } else {
        self.textField.textAlignment = UITextAlignmentLeft;
    }

    self.textField.frame = editFrame;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    self.textField.enabled = editing;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        [self.textField becomeFirstResponder];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        [self.textField becomeFirstResponder];
    }
}

@end

And with the setup on the table view you have the expected behavior of having a string displayed in normal mode and editable when in edit mode

self.tableView.allowsSelection = NO;
self.tableView.allowsSelectionDuringEditing = YES;

Upvotes: 0

Krishna
Krishna

Reputation: 241

--- I found the answer:

I've removed the enable/disable code from following methods:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

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

and added the following in custom cell.m

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:YES];

    if(editing){
        textField.enabled = YES;
    }else{
       textField.enabled = NO;
    }

    [UIView commitAnimations];
}

it's working now. i'm not sure whether this is correct approach or not but its working fine.

Upvotes: 3

CodaFi
CodaFi

Reputation: 43330

EDIT: my bad, didn't properly read the method signatures. What you're asking is quite hard to do, because all textfields are most likely subviews of the cell or it's contentView. What you need to do is disable the cell's selectionStyle, return nil for everything in willSelect... Then allow selection during editing.

Upvotes: 0

Related Questions