【发布时间】:2015-08-25 18:42:42
【问题描述】:
我遇到的问题是,当我滚动表格视图时,如果文本字段为空白(并且显示占位符值),则行显示相应的占位符值就好了。但是,如果我开始浏览表格视图并在文本字段中输入文本,一旦我向下滚动,之前的文本字段值就会开始显示在表格视图下方的其他文本字段中。我认为它与单元格标识符有关,但它似乎适用于我的标签和 uitextfield 占位符,而不是文本字段用户文本输入。有什么想法吗?这是代码...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.headerLabel.text = @"some string";
cell.textField.placeholder = @"some other string placeholder,";
cell.textField.delegate = self;
return cell;
}
这里是 CustomCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
headerLabel =[[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.translatesAutoresizingMaskIntoConstraints = NO;
headerLabel.textColor = [UIColor colorWithRed:155.0/255.0 green:155.0/255.0 blue:155.0/255.0 alpha:1.0];
headerLabel.font = [UIFont fontWithName:@"Helvetica Neue Reg" size:15];
textField = [[UITextField alloc] initWithFrame:CGRectZero];
textField.textColor = [UIColor colorWithRed:13.0/255.0 green:122/255.0 blue:255.0/255.0 alpha:1.0];
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.font = [UIFont fontWithName:@"Helvetica Neue" size:15];
[self.contentView addSubview:headerLabel];
[self.contentView addSubview:textField];
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:headerLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:27 ] ] ;
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:headerLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTop
multiplier:1
constant:10 ] ] ;
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:27 ] ] ;
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:headerLabel
attribute:NSLayoutAttributeBottom
multiplier:1
constant:5 ] ] ;
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:-27 ] ] ;
}
return self;
}
【问题讨论】:
-
您需要在文本更改时保存文本,并且您需要在
cellForRowAtIndexPath:方法中设置每个文本字段的text属性。
标签: ios objective-c uitableview