Reputation: 2284
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.
Upvotes: 1
Views: 11734
Reputation: 3093
You have to create custom cell for this. like this,
in CustomCellTableViewCell.h file
#import <UIKit/UIKit.h>
@interface CustomCellTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *lblUnit;
@end
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
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;
Upvotes: -6
Reputation: 178
NSTextAlignmentCenter does work if you use initWithStyle:UITableViewCellStyleDefault.
Upvotes: 0
Reputation: 3377
Create a custom UITableViewCell
subclass and override the layoutSubviews
method.
Upvotes: 8