2014-11-03 4 views
2

Мне нужно выровнять текст в ячейке справа.выровнять текст в UITableViewCell справа

, но даже когда я устанавливаю направо текст выравнивать влево, как показано на скриншоте любая идея, почему это странное поведение клетки

cell.textLabel.textAlignment = NSTextAlignmentRight; 
cell.detailTextLabel.textAlignment = NSTextAlignmentRight; 

После прочтения связанные вопросы и ответы на StackOverflow I не мог найти решение моей проблемы

enter image description here

+0

пожалуйста, добавьте полный метод cellForRowAtIndexPath – Roma

ответ

0

Единственное решение, чтобы решить эту проблему, чтобы создать custuom клетки сначала создать MTTableViewCell.h


#import <UIKit/UIKit.h> 

@interface MTTableViewCell : UITableViewCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; 
@end 

т курица создать MTTableViewCell.m


#import "MTTableViewCell.h" 

@implementation MTTableViewCell 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
if (self) { 
// Initialization code 
} 
return self; 
} 


- (void) layoutSubviews { 
[super layoutSubviews]; 
//self.textLabel.frame = CGRectMake(200, 0, 310, 20); 
self.detailTextLabel.textAlignment = NSTextAlignmentRight; 
self.textLabel.textAlignment = NSTextAlignmentRight; 

//self.textLabel.frame = CGRectMake(100, 0, 310, 20); 
CGRect adjustedFrame = self.textLabel.frame; 
adjustedFrame.origin.x -= 10.0f; 
self.textLabel.frame = adjustedFrame; 
adjustedFrame = self.detailTextLabel.frame; 
adjustedFrame.origin.x -= 10.0f; 
self.detailTextLabel.frame = adjustedFrame; 


{ 
CGRect frame = self.textLabel.frame; 
frame.size.width = CGRectGetWidth(self.frame); 

if(self.accessoryView != nil){ 
if (IDIOM == IPAD) { 
frame.size.width = frame.size.width - 210; 
} else { 
frame.size.width = frame.size.width - 120; 
} 

frame.origin.x +=30; 
} 
else{ 
frame.size.width = frame.size.width - 50; 
frame.origin.x += 30.0; 
} 
self.textLabel.frame = frame; 
} 

{ 
CGRect frame = self.detailTextLabel.frame; 
frame.size.width = CGRectGetWidth(self.frame); 

if(self.accessoryView != nil){ 
if (IDIOM == IPAD) { 
frame.size.width = frame.size.width - 210; 
} else { 
frame.size.width = frame.size.width - 120; 
} 
frame.origin.x += 30.0; 
} 
else{ 
frame.size.width = frame.size.width - 50; 
frame.origin.x += 30.0; 
} 
self.detailTextLabel.frame = frame; 
} 


} 

@end 

затем в камере

cell = [[[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
[cell.textLabel setNumberOfLines:2]; 
[cell.detailTextLabel setNumberOfLines:3]; 
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

, который

1

ваше кодирование хорошо, и нужно изменить cell.textLabel.frame size to right side в моем выборе является использование пользовательских лаборатории el

+0

я пытаюсь изменить размер кадра, но это не меняет любую вещь –

+0

RU используется автоматический макет или автоматического изменения, если и используется автоматическая раскладка удалить старые ограничения и добавить новые ограничения, если и автоматического изменения, изменить положение на правый угол –

2

Невозможно изменить рамочку titleLabel of Cell. Вместо этого вы можете добавить пользовательскую метку в contentView ячейки.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString * cellIdentifier = @"Cell"; 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell==nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    UILabel *lblStatus = [[UILabel alloc] initWithFrame:CGRectMake(12, 8, 175, 15)]; 
    lblStatus.backgroundColor = [UIColor clearColor]; 
    [lblStatus setTag:101]; 
    [cell.contentView addSubview:lblStatus]; 

} 
UILabel *lbl1 = (UILabel *)[cell.contentView viewWithTag:101]; 
[email protected]"set text"; 
} 
return cell; 
} 

Вы также можете достичь этого прототипом ячейки. here is a good tutorial of prototype cell.

2

лучше использовать пользовательские метки и добавить его в cell.contentView следующим ниже

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

if (cell == nil) 
{ 

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] ; 

    } 

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; 
label.textAlignment = NSTextAlignmentRight; 
[cell.contentView addSubview:label]; 
label.backgroundColor = [UIColor purpleColor]; 
label.textColor = [UIColor blackColor]; 
label.text = [titlesArray objectAtIndex:indexPath.row]; 

return cell; 

} 
0

Я использовал ниже действия кнопки. Кнопка размещена на стороне стола.

- (IBAction)rightBtn:(id)sender { 

cell.textLabel.textAlignment=UITextAlignmentRight; 
    } 
0

Фактически в этом случае лучший подход - создать свою собственную камеру и управлять ею. Но в случае, если вам нужна такая функциональность, вы могли бы попытаться сделать что-то вроде

cell?.transform = CGAffineTransform(scaleX:-1, y:1) 
cell?.textLabel?.transform = CGAffineTransform(scaleX:-1, y:1) 
cell?.detailTextLabel?.transform = CGAffineTransform(scaleX:-1, y:1) 

Инвертирует «видимой» части клетки, как вам требуется.