【问题标题】:UITextField Text not refreshingUITextField 文本不刷新
【发布时间】:2021-01-03 11:23:08
【问题描述】:

TableViewCell 中的文本字段出现了奇怪的行为。当我在开始时设置它的文本时,它显示得很好,但是当我开始编辑它时,旧文本不会被清除并覆盖它。见附图。我的代码是:

_meterTF = [[UITextField alloc] init];
_meterTF.backgroundColor = [UIColor clearColor];
_meterTF.textColor = [UIColor grayColor];
_meterTF.font = WO_DETAIL_NORMAL_FONT;
_meterTF.tag = 1;
_meterTF.textAlignment = NSTextAlignmentRight;
_meterTF.layer.borderColor = GRAY_BORDER_COLOR.CGColor;
_meterTF.layer.borderWidth = borderWidth;
[_meterTF setRightViewMode:UITextFieldViewModeAlways];
[_meterTF setRightView:[[UIView alloc] initWithFrame:CGRectMake(-5,0, 10, 10)]];
_meterTF.delegate = self;
[_meterTF setKeyboardType:UIKeyboardTypeNumberPad];
if (!self.isEditable) {
    _meterTF.layer.borderColor = [UIColor clearColor].CGColor;
    _meterTF.enabled = NO;
    
} 

这里是 CellForRow 方法。 [cell.meterView initSubview] 这一行实际上添加了那个textField

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *meterCellIdentifier = @"meterCell";
    
    DISMeterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:meterCellIdentifier];
    if (!cell) {
        cell = [[DISMeterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:meterCellIdentifier];
        [cell.historyButon addTarget:self action:@selector(showMeterPreviousHistory:) forControlEvents:UIControlEventTouchUpInside];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.meterView.isEditable = (self.isEditMode && self.currentWorkOrderSegment.isEditable) || self.isTechReview;
        [cell.meterView initSubview];

    }
    cell.meterView.tag = indexPath.row;
    cell.meterView.section = indexPath.section;
    cell.historyButon.row = indexPath.row;
    cell.historyButon.section = indexPath.section;
    cell.historyButon.enabled = self.currentWorkOrderSegment.isEditable;
    cell.selectionStyle = UITableViewScrollPositionNone;
    [cell.meterView resetView];
    if (self.isEditMode) {
        [cell.meterView renderMeter:self.currentWorkOrderSegment.orderEquipmentMeterListViewModels[self.equipmentIndex].orderEquipmentMeters[indexPath.row] withIndex:indexPath.row];

    }
    else
    {
        if(indexPath.section < self.currentWorkOrderSegment.orderEquipmentMeterListViewModels.count && indexPath.row < self.currentWorkOrderSegment.orderEquipmentMeterListViewModels[indexPath.section].orderEquipmentMeters.count)
            [cell.meterView renderMeter:self.currentWorkOrderSegment.orderEquipmentMeterListViewModels[indexPath.section].orderEquipmentMeters[indexPath.row] withIndex:indexPath.row];

    }
    cell.backgroundColor = [UIColor clearColor];
    cell.meterView.delegate = self;
    return cell;
}

【问题讨论】:

  • 看起来您正在添加 另一个 文本字段。您在哪里执行您发布的代码,您在哪里/何时将_meterTF 添加到视图中?
  • 它看起来像创建了几次文本字段。您应该创建一个文本字段一次,并且在 cellForRowAt:IndexPath 方法中将单元格出列时,您只需要设置文本字段的值,而不是创建新的文本字段..
  • 细胞被重复使用。你错过了这个概念......
  • 更新问题
  • 如何将_meterTF 添加到cell

标签: ios objective-c iphone uitableview uitextfield


【解决方案1】:

我认为你需要使用 dequeueReusableCell

guard let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CellIdentifier.tableViewCell.MyProfileTopTableViewCell, for: indexPath) as? MyProfileTopTableViewCell else { return UITableViewCell() }

并在单元格中添加 Textfield didchange 委托,因此每当用户更新文本字段数据时,您需要维护数组/字典并更新 textfieldata 手风琴,并且在 tableview cellforrow 中只需在单元格中显示更新的文本字段数据。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CellIdentifier.tableViewCell.MyProfileTopTableViewCell, for: indexPath) as? MyProfileTopTableViewCell else { return UITableViewCell() }
let objects = self.textfieldData[indexpath.row]
cell.textfield?.text =  objects
cell.delegate = self
}

class Yourcontroller: textfieldChange
 {
func updateTextfield(text: UITextfield)
{
  self.textfieldData[text.tag] =  text.text
}
}

protocol textfieldChange: class
{
func updateTextfield(text: UITextfield)
}

class MyProfileTopTableViewCell: UITableViewCell {
   @IBOutlet weak var txtfield: UITextfield?
func updateTextifledcell(data: String, indexPath)
{
self. txtfield =  data
self. txtfield.tag = indexPath.tag
}
var delgate: textfieldChange?
override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self. txtfield.delegate = self
self.textField?.addTarget(self, action: #selector(txtEditing), for: .editingChanged)
    }
}
@objc func txtEditing(_ txt : UITextField){
self. delgate. updateTextfield(textView)
}

我希望你明白我的意思,它会解决这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    相关资源
    最近更新 更多