【发布时间】: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