将文本与 UITableViewCell 对齐的问题 [英] Problems aligning text with UITableViewCell

查看:39
本文介绍了将文本与 UITableViewCell 对齐的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableView 中有一个普通单元格,当我想将它的文本向右对齐时,我使用:

I have a normal cell in tableView, when I want to align it text to right I use:

if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];        
     cell.textLabel.textAlignment = UITextAlignmentRight;           
}

但现在我想使用 UITableViewCell 来显示 detailTextLabel 我试图再次将它向右对齐但没有任何反应:

But now I want to use the UITableViewCell for displaying also detailTextLabel I tried to align it to right again but nothing happens:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textAlignment = UITextAlignmentRight;
    cell.detailTextLabel.textAlignment = UITextAlignmentRight;    
}

推荐答案

如果要自定义单元格,请将您自己的 UILabel 添加为单元格的子视图:

add your own UILabel as a subview to the cell if you want to customize it:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textAlignment = UITextAlignmentRight;
UILabel *subLabel = [UILabel alloc] initWithFrame:CGRectMake(x,y,w,h)]; //put in the frame variables you desire
subLabel.textAlignment = UITextAlignmentRight;
subLabel.tag = 20;
subLabel.text = @"my text";
[cell.contentView addSubview:subLabel];
[subLabel release]; //dont call this if you are using ARC

...

然后您稍后访问标签:

[[cell.contentView viewWithTag:20] setText:@"new text"];

希望这会有所帮助.

这篇关于将文本与 UITableViewCell 对齐的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆