2012-03-10 61 views
2

我有一个自定义的UITableViewCell,它有1个文本框和一个标签。我只想在tableview处于编辑模式时启用文本字段编辑。在UITableViewCell中启用/禁用编辑模式

我正在使用以下方法来启用文本字段编辑模式并禁用文本字段编辑模式。但这不起作用。我不确定这是否正确。如果这不是正确的方法,你能让我知道如何禁用启用文本字段?

- (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; 

也是我有下面几行:

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. 

请帮帮忙!

回答

3

---我找到了答案:

我已经移除以下方法启用/禁用代码:

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

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

,并添加自定义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]; 
} 

它现在正在工作。我不确定这是否正确,但它的工作正常。

0

编辑:我的坏,没有正确读取方法签名。你所要求的是很难做到的,因为所有的文本字段很可能是单元格的子视图或它的contentView。你需要做的是禁用单元格的selectionStyle,在willSelect中返回nil,然后允许在编辑过程中进行选择。

0

因为我一直在寻找同样有一段时间了,下面是完整的代码实现它

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 

并与在表格视图中进行设置时,您具有在正常模式下显示字符串并在编辑模式下可编辑的预期行为

self.tableView.allowsSelection = NO; 
self.tableView.allowsSelectionDuringEditing = YES; 
相关问题