【问题标题】:NSTextAligment applying only after editing textfieldNSTextAligment 仅在编辑文本字段后应用
【发布时间】:2014-05-23 14:52:06
【问题描述】:

我创建了表格视图单元格,其中包含我的自定义文本字段,UITextField 的子类。当我将文本字段添加到我的单元格时,我设置了

textField.textAlignment = NSTextAlignmentCenter;

我在文本字段上有占位符。当我的单元格出现时,它的对齐方式是左侧和中心之间的平均值。然后在编辑后成为中心。创建单元格后我需要中心对齐。谁能帮忙解决这个问题?

编辑

我的 CellForRowAtIndexPath 方法:

- (CTKMultiColumnTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.section];
    CTKMultiColumnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[CTKMultiColumnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        [cell setFrameWithRect:CGRectMake(0, 0, tableView.frame.size.width, 34)];
    }
    cell.textField.placeholder = [_placeholders objectAtIndex:tableView.tag];
    cell.delegate = self;
    cell.relatedTableView = tableView;

    return cell;
}

以及 willDisplayCell 方法的一部分

- (void)tableView:(UITableView *)tableView willDisplayCell:(CTKMultiColumnTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (tableView.tag) {

        case 4:
            cell.textField.leftView = nil;
            cell.textField.rightView = nil;
            cell.textField.textAlignment = NSTextAlignmentCenter;
            cell.textField.keyboardType = UIKeyboardTypeNumberPad;
            cell.isPickable = NO;
            cell.valueLimitMin = 0;
            cell.valueLimitMax = 1000;

            if (((CTKRoom *)[_roomsArray objectAtIndex:indexPath.section]).heatedArea) {
                cell.textField.text = [NSString stringWithFormat:@"%@", ((CTKRoom *)[_roomsArray objectAtIndex:indexPath.section]).heatedArea];
            }
            else cell.textField.text = @"";
            break;


}

这部分是为我的单元格设置属性的示例

【问题讨论】:

  • 你能展示你是如何创建你的单元格的吗?也许你的 cellForRowAtIndexPath 方法。
  • 尝试在YourTextField的init方法中设置textAlignment
  • @Seryozha 我无法在 TextField 初始化方法中执行此操作,因为我在许多不同的表格中使用此 TextField 但只需要在一个表格中居中对齐。
  • @Justafinger 我编辑了问题
  • 在设置占位符之前,您是否尝试过在 cellForRowAtIndexPath 中设置对齐方式?奇怪的部分是关于“左侧和中心之间的平均值”,您确实意识到如果您的占位符很长,它将从左侧和中心之间的某个位置开始,对吗?

标签: ios uitextfield


【解决方案1】:

我解决了我的问题。原因是

myTextField.rightViewMode = UITextFieldViewModeAlways;

在我的情况下,正确的视图是必要的,所以在不需要时我设置

myTextField.rightView.frame = CGRectMake(0, 0, 1, 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2019-11-10
    • 2015-12-03
    • 1970-01-01
    • 2011-05-26
    • 2012-10-09
    • 2020-11-27
    相关资源
    最近更新 更多