【发布时间】:2014-02-17 13:26:35
【问题描述】:
我有一个包含三个表格视图单元格的表格视图。 在单元格配置期间,我向每个单元格添加了一个 UITextField,并且我还在所有文本字段上设置了占位符值。 当表格视图加载时,最终结果如下所示:
我遇到的问题是,当我将任何单元格“从屏幕上”滚动出来时,它们再次出现时,占位符文本变得越来越暗,如下所示:
当我尝试通过在其中键入名称或以编程方式更改最后一个单元格上的占位符值来更改 UITextField 字符串值时,这些属性的旧值将保留,而新值将覆盖在单元格中,例如这个:
以下是负责配置这些单元的方法:
- (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];
}
如您所见,我尝试了各种方法,例如在向单元格添加新视图之前删除所有子视图或在单元格和 UITextView 上设置 -[UIView setClearsContextBeforeDrawing:YES] 甚至将占位符值设置为没有成功。
任何指针将不胜感激!
【问题讨论】:
标签: ios objective-c uitableview uitextfield