我有一个自定义的UITableViewCell,它有一个文本字段和一个标签。我想仅在tableview处于编辑模式时启用文本字段编辑。
我正在使用以下方法启用文本字段编辑模式并禁用文本字段编辑模式。但这不起作用。我不确定这是否是正确的方法。如果这不是正确的方法,你能告诉我如何禁用启用文本字段吗?
- (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.
请帮忙!
答案 0 :(得分: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];
}
它现在正在运作。我不确定这是否正确,但工作正常。
答案 1 :(得分:0)
答案 2 :(得分: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;