我有一个带有三个表格视图单元格的表格视图。在单元格配置期间,我向每个单元格添加一个UITextField,并且还在所有文本字段上设置了占位符值。加载表视图时,最终结果如下所示:
我遇到的问题是,当我滚动出现任何“离屏”的单元格时,当它们再次出现时,占位符文本变得越来越暗,如下所示:
然后,当我尝试通过在其中键入名称或以编程方式更改最后一个单元格上的占位符值来更改UITextField字符串值时,这些属性的旧值会保留,而新值将覆盖在该单元格中,如下所示:
Here are the methods responsible for configuring those cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
[self configureCell:cell forIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
for (UIView *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
}
cell.backgroundColor = [UIColor whiteColor];
cell.autoresizesSubviews = YES;
cell.clearsContextBeforeDrawing = YES;
CGRect textFieldFrame = cell.bounds;
textFieldFrame.origin.x += 10;
textFieldFrame.size.width -= 10;
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];
textField.adjustsFontSizeToFitWidth = YES;
textField.textColor = [UIColor lightGrayColor];
textField.enabled = YES;
textField.userInteractionEnabled = NO;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textField.clearsContextBeforeDrawing = YES;
textField.clearsOnBeginEditing = YES;
if (indexPath.section == ATTAddNewTimerSectionName) {
textField.placeholder = @"Name";
textField.userInteractionEnabled = YES;
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
} else if (indexPath.section == ATTAddNewTimerSectionDate) {
textField.placeholder = @"Date/Time";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (indexPath.section == ATTAddNewTimerSectionCalendar) {
if (self.userCalendarEvent == nil) {
textField.placeholder = @"See list of calendar events";
} else {
textField.placeholder = nil;
textField.placeholder = self.userCalendarEvent.title;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[cell addSubview:textField];
}
As you can see, I have tried various things, like removing all the subviews before adding a new view to the cell or setting -[UIView setClearsContextBeforeDrawing:YES] on both the cell and the UITextView and even setting the placeholder value to nil without success.
Any pointers would be much appreciated!
You are making a classic error that so many people make. Cells get reused as a table is scrolled.
As written, your code keeps creating and adding a new text field every time the cell is used. So you are seeing the result of having multiple text fields in a cell.
You want to only add one text field to a cell. Update your code so it only adds the text field if it isn't there already.
似乎您在代码中存在逻辑问题。您可以将文本字段直接添加到单元格中,但是尝试将其从单元格中删除contentView
。
更改此:
[cell addSubview:textField];
至:
[cell.contentView addSubview:textField];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句