【发布时间】:2015-03-14 20:30:00
【问题描述】:
我实际上正在填写注册表,但遇到了一些麻烦。
我在单元格中使用了 UITextField,并且在占位符中写入了一个字符串。在我尝试将 UITextField.textAlignment 居中之前,一切都运行良好。它以某种方式设法不使其居中。我检查了它并做了一些测试,我认为这可能是由于约束的问题(我真的希望我的表单适用于 iphone 4s、5、6 和 6+ 屏幕)。当我在 iphone 6 上时,它看起来几乎居中,但是你会看到字符串越来越不居中,如果你切换到 iphone 5 然后是 iphone 4s,它会转向右侧。到目前为止,我只使用了两个高度约束。
现在“好的”事情是当我滚动我的 tableView 时,如果我的单元格看不见,一旦我重新加载我的单元格,占位符字符串会第二次出现,但在正确的位置(这次是真正的中心)旧的错位占位符字符串。我希望这个占位符在我第一次加载表格时出现滚动时显示,因为它似乎是我想要得到的确切结果。但这仅在我尝试将占位符居中时出现,如果我不这样做,则不会显示第二个占位符字符串。我给你两个图片链接,因为我的英语很糟糕,你们这样可能会更好地理解。
+= 当我滚动 tableView 时,我的代码没有发生任何事情,它运行相同的 CellForRowAtIndexPath 但仍然有不同的结果。
感谢你们带给我的所有建议和帮助
这是我正在运行的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"id";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if (indexPath.section == 0){
self.LastName = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
self.LastName.placeholder= @"Last Name";
self.LastName.autocorrectionType = UITextAutocorrectionTypeNo;
self.LastName.textAlignment = NSTextAlignmentCenter;
self.LastName.delegate = self;
[self.LastName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.LastName setTextColor:[UIColor whiteColor]];
[cell.contentView addSubview:self.LastName];
[cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
}
else if (indexPath.section == 1)
{
self.Name = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
self.Name.placeholder= @"Name";
self.Name.autocorrectionType = UITextAutocorrectionTypeNo;
self.Name.textAlignment = NSTextAlignmentCenter;
self.Name.delegate = self;
[self.Name setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.Name setTextColor:[UIColor whiteColor]];
[cell.contentView addSubview:self.Name];
[cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
}
else if (indexPath.section == 2)
{
self.Email = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
self.Email.placeholder= @"Email";
self.Email.autocorrectionType = UITextAutocorrectionTypeNo;
self.Email.textAlignment = NSTextAlignmentCenter;
self.Email.delegate = self;
[self.Email setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.Email setTextColor:[UIColor whiteColor]];
[cell.contentView addSubview:self.Email];
[cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
}
else if (indexPath.section == 3)
{
self.Password = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
self.Password.placeholder= @"Password";
self.Password.autocorrectionType = UITextAutocorrectionTypeNo;
self.Password.textAlignment = NSTextAlignmentCenter;
self.Password.delegate = self;
[self.Password setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.Password setTextColor:[UIColor whiteColor]];
[cell.contentView addSubview:self.Password];
[cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
}
else if (indexPath.section == 4)
{
self.RepetePassword = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
self.RepetePassword.placeholder= @"Repeat password";
self.RepetePassword.autocorrectionType = UITextAutocorrectionTypeNo;
self.RepetePassword.textAlignment = NSTextAlignmentCenter;
self.RepetePassword.delegate = self;
[self.RepetePassword setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.RepetePassword setTextColor:[UIColor whiteColor]];
[cell.contentView addSubview:self.RepetePassword];
[cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
}
else if (indexPath.section == 5)
{
cell.textLabel.text = @"Register";
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];
[cell.contentView.layer setBackgroundColor:[UIColor greenColor].CGColor];
[cell.contentView.layer setBorderColor:[UIColor clearColor].CGColor];
}
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new];
cell.selectedBackgroundView = [UIView new];
[cell.contentView.layer setBorderWidth:1.0f];
[cell.contentView.layer setCornerRadius:20.0f];
[cell.contentView.layer setMasksToBounds:YES];
return cell;
}
【问题讨论】:
标签: ios objective-c uitableview uitextfield constraints