xcode 如何在tableview单元格iphone中设置textLabel的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9061276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set position of textLabel in a tableview cell iphone
提问by iOS dev
- In my table view i was using UITableviewCEllStyleValue1:
- 在我的表格视图中,我使用了 UITableviewCellStyleValue1:
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = @"Hai";
cell.detailTextLabel.text = @"Hello";
cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textAlignment = UITextAlignmentLeft;
return cell;
}
Here two labels text labe, detailTextLabel are displayed.
but not in the position what i want.
I need to display Textlable from starting position of the cell (0,0) and immidiate right side position is detailTextlabel no gap between two labels.
- I need a solution with out using Allignment. what to do?
这里显示了两个标签文本标签,detailTextLabel。
但不是我想要的位置。
我需要从单元格 (0,0) 的起始位置显示 Textlable,并且右侧位置是 detailTextlabel 两个标签之间没有间隙。
- 我需要一个不使用对齐的解决方案。该怎么办?
采纳答案by Dipen Chudasama
You have to create custom cell for this. like this,
您必须为此创建自定义单元格。像这样,
in CustomCellTableViewCell.h file
在 CustomCellTableViewCell.h 文件中
#import <UIKit/UIKit.h>
@interface CustomCellTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *lblUnit;
@end
in CustomCellTableViewCell.m file
in CustomCellTableViewCell.m file
#import "CustomCellTableViewCell.h"
@implementation CustomCellTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.userInteractionEnabled = YES;
if (self) {
// Initialization code
self.lblUnit = [[UILabel alloc]init];
[self.lblUnit setTextColor:[UIColor whiteColor]];
[self.lblUnit setFont:[UIFont fontWithName:@"Roboto-Light" size:30.0f]];
// [self.lblUnit setFont:[UIFont systemFontOfSize:18.0]];
self.lblUnit.textAlignment = UIBaselineAdjustmentAlignCenters;
self.lblUnit.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:self.lblUnit];
}
return self;
}
@end
in your Tableview CellForRowAtIndexpath write below code
in your Tableview CellForRowAtIndexpath write below code
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell==nil){
cell = [[CustomCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.lblUnit.frame = CGRectMake(0, 10, 150, 40);
cell.lblUnit.text = @"Hai";
return cell;
回答by Jakob W
Create a custom UITableViewCell
subclass and override the layoutSubviews
method.
创建自定义UITableViewCell
子类并覆盖该layoutSubviews
方法。
回答by Shankar Aware
NSTextAlignmentCenter does work if you use initWithStyle:UITableViewCellStyleDefault.
如果您使用 initWithStyle:UITableViewCellStyleDefault,NSTextAlignmentCenter 确实有效。