当单元格样式为默认时,NSTextAlignment
工作,但是当我将其更改为字幕时,它不起作用。NSTextAlignmentCenter不能使用UITableViewCellStyleSubtitle
cell.textLabel.textAlignment=NSTextAlignmentCenter;
当单元格样式为默认时,NSTextAlignment
工作,但是当我将其更改为字幕时,它不起作用。NSTextAlignmentCenter不能使用UITableViewCellStyleSubtitle
cell.textLabel.textAlignment=NSTextAlignmentCenter;
您不能更改单元格字幕标签的NSTextAlignment
。原因是这个标签只与文本一样宽。如果你不相信我的话,你可以尝试设置它的背景色:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.backgroundColor = [UIColor orangeColor];
}
所以,你可以看到,你的UILabel
根据它的文本改变其宽度。
官方文件也针对其对齐:
一种横跨顶部的左对齐标签和 左对齐标签在它下面更小的灰色文本的单元格样式。 iPod应用程序 使用此风格的单元格。
所以,我可以建议不同的方式来实现自己的目标:
第一种方式
您可以创建以下教程自己的自定义单元格:
您可以自己添加标题标签和副标题并设置其所有属性。
方式二
这里是另一个解决方法,你可以使用它,如果你不想来实现自定义的细胞。只需使用下面的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *myLabel;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(yourX, yourY, yourWidth, yourHeight)];
myLabel.tag = 1;
myLabel.textAlignment= UITextAlignmentCenter;
[cell.contentView addSubview:myLabel];
}
myLabel = (UILabel*)[cell.contentView viewWithTag:1];
//Add text to it
myLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
第三条道路
您可以实现的方法,渲染之后,将改变你的细胞的副标题标签的文本对齐方式。这绝对不是最好的解决办法,但你可以尝试,如果你想:
- (void) updateCenteringForTextLabelInCell:(UITableViewCell*)cell
{
UILabel *myLabel = cell.textLabel;
myLabel.frame = CGRectMake(myLabel.frame.origin.x, myLabel.frame.origin.y, cell.contentView.frame.size.width, myLabel.frame.size.height);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//use your already implemented code
[self performSelector:@selector(updateCenteringForTextLabelInCell:) withObject:cell afterDelay:0.05];
}
摘要:
我建议你使用第一种方式,因为所有的人都有点棘手。除此之外,您可以稍后在自定义单元格中添加很多不同的元素,这可能非常有用。
谢谢兄弟。这是非常非常丰富和有益的。 – user2096064
你可以投票的答案;) – etolstoy
投票需要声誉。我还没有任何:( – user2096064