Как установить положение textLabel в ячейке tableview iphone - PullRequest
1 голос
/ 30 января 2012
  • В моем табличном представлении я использовал 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;

        }

  • Здесь отображаются две метки текста labe, detailTextLabel.

  • но не в том положении, в котором я хочу.

  • Мне нужно отобразить Textlable из начальной позиции ячейки (0,0) и imidiateПравая позиция - деталь. Текстовый ярлык, между двумя метками нет зазора.

  • Мне нужно решение без использования Allignment.что делать?

Ответы [ 3 ]

8 голосов
/ 30 января 2012

Создайте пользовательский подкласс UITableViewCell и переопределите метод layoutSubviews.

0 голосов
/ 12 июня 2014

NSTextAlignmentCenter работает, если вы используете initWithStyle: UITableViewCellStyleDefault.

0 голосов
/ 30 января 2012

Для этого вам нужно создать собственную ячейку. как это,

в файле CustomCellTableViewCell.h

#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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...