0

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;           
}

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;    
}
El Developer
  • 3,345
  • 1
  • 21
  • 40
vadim
  • 119
  • 3
  • 14

4 Answers4

3

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

...

then you access the label later:

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

hope this helps.

KDaker
  • 5,899
  • 5
  • 31
  • 44
  • I agree. The built in labels have unexpected behaviors sometimes with alignment and positioning. I know in the past, I ran into an issue with how the built in labels were auto-realigned when resizing the cell, but a created label would not auto-realign (which was the behavior I ACTUALLY wanted). – MikeS Aug 30 '12 at 19:08
  • @KDaker Is it he only result for that? because I want to insert there dynamic text,that can be different size,and I need to initialize it at start with some params of CGRectMake that I dont know while I am getting the text – vadim Aug 31 '12 at 09:25
  • are you changing the text while the cell is in the view? or when you go back to it? because you can still assign it text without making it an instance variable by tagging it. – KDaker Aug 31 '12 at 16:12
  • Why you need: `[[cell.contentView viewWithTag:20] setText:@"new text"];` for? if you set the text in: `subLabel.text = @"my text";` – vadim Sep 03 '12 at 06:05
  • if youd like to change the text later on (dynamically)... if your not changing the text you dont need it! – KDaker Sep 03 '12 at 06:12
1

Did you check if the length of your detailTextLabel is not as big as its frame? If the frame is just big enough to fit the text exactly, then it might look like UITextAlignmentRight isn't working.

Ravi
  • 7,929
  • 6
  • 38
  • 48
0

Try implementing the tableView:willDisplayCell:forRowAtIndexPath: delegate method and put your re-alignment code there!

Nenad M
  • 3,055
  • 20
  • 26
0

It is impossible to change frame for textLabel and detailTextLabel in UITableViewCellStyleSubtitle table cell in a simple way.

It is better to subclass your cell to make custom layout. But also you can implement a small hack using

performSelector:withObject:afterDelay:

with zero delay for changing textLabel frame right after layoutSubviews:

[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0];

At alignText: you can redefine textLabel/detalTextLabel frames.

See implementation at here

Community
  • 1
  • 1
malex
  • 9,874
  • 3
  • 56
  • 77