在UITableViewCell中禁用UITextField

时间:2014-07-22 18:23:54

标签: objective-c ios7

我在UITextField中有一个UITableViewCell

即使我设置 -

textField.userInteractionEnabled = NO;
textField.enabled = NO

但是当我点击包含textField的表格单元格时,键盘会出现在文本字段中。

为什么会发生这种情况,我该如何预防?

编辑:奇怪的是,当我第一次在文本字段中设置一些文本时,就会发生这种情况。当textfield为空时,它是不可编辑的。

编辑:cellForRowAtIndexPath的代码 -

cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

cell.accessoryType = UITableViewCellAccessoryNone;

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];

textField.font = [UIFont systemFontOfSize:15];
textField.textColor = [UIColor blackColor];

UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:@{NSForegroundColorAttributeName : placeholderColor}];
textField.keyboardType = [self getKeyboardTyeForIndexPath:indexPath];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.textAlignment = NSTextAlignmentLeft;
textField.autocapitalizationType = [self getAutocapitaliztionTypeForIndexPath:indexPath];
textField.tag = 1;

if (_editingNotAllowed) {
    [textField setText:[self getTextForTextFieldWithIndexPath:indexPath]];
    [textField setUserInteractionEnabled:NO];
    textField.enabled = NO;
} else {
    [textField setUserInteractionEnabled:YES];
}        
[cell.contentView addSubview:textField];

1 个答案:

答案 0 :(得分:0)

您应该创建一个UITableViewCell,如此repo中所示:)

https://github.com/breeno/EditingUITableView

使用您的自定义UITableViewCell:

if(condition){ // Check the row must be TextField
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: nil];
    if(!cell){
         cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
    }
    cell.label.text = @"Title Row"; //UITextLabel Title
    UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
    cell.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:@{NSForegroundColorAttributeName : placeholderColor}];
    cell.textField.returnKeyType = UIReturnKeyDone;
    cell.textField.backgroundColor = [UIColor clearColor];
    cell.textField.textAlignment = NSTextAlignmentLeft;
    cell.textField.tag = 1;
} else { // Normal UITableViewCell 
}