【发布时间】: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