bao bao - 1 year ago 48
iOS Question

How to change the width of tableView cell textLabel

I want to add 2 imageViews on the Left and Right and a cell textLabel on the Center on TableView.

The 2 imageViews has different role and action.
(Being able to touch, hidden and not hidden)

Now the left side imageView is on the top of the center textLabel.
So I want to change the textLabel's width and x position not to be on the top of imageViews.

I tried this code but it didn't work.

CGRect frame = CGRectMake(40, 0, 200, 40);
cell.textLabel.frame = frame;


and I also tried to add UILabel on the tableView.
But I will implement TableViewCell animation and if I add UILabel, it doesn't work.
So I don't use this way.

How can I solve it?

here is the code.

- (void)viewDidLoad {

tableView = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

[self.view addSubview: self.tableView];
self.tableView.backgroundColor = [UIColor clearColor];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];

return cell;
}

- (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{

NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor blueColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont fontWithName:@"HiraKakuProN-W3" size:15];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.minimumScaleFactor = 10.0f;
cell.textLabel.numberOfLines = 0;
}

Answer Source

you need to override the layoutSubviews for your UItableviewCell class

- (void)layoutSubviews {
[super layoutSubviews];

CGSize size = self.bounds.size;
self.textLabel.frame =   CGRectMake(2.0f, 4.0f, size.width, size.height);  // or customize 
self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}

Choice-2

crate the UILabel and subview to cell.contentView

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 200, 30)];
label.text = @"Test";
[cell.contentView addSubview:label];

Update

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];



  if (cell == nil){
        myCellView = [[UITableViewCell alloc]
                      initWithStyle:UITableViewCellStyleDefault
                      reuseIdentifier:@"Cell"];

        UILabel *orderLbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 13, 176, 21)];
        [orderLbl setTag:1];
        orderLbl.textColor = [UIColor whiteColor];
        [orderLbl setBackgroundColor:[UIColor clearColor]];
        [orderLbl setTextAlignment:NSTextAlignmentCenter];

        [cell.contentView addSubview:orderLbl];
  }

        [(UILabel *)[cell.contentView viewWithTag:1] setText:[NSString stringWithFormat:@"test"]];
      return cell;
  }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download