简体   繁体   中英

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

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

Better you can use Custom Textlabel with your desired frame requirement.

Give a tag for the textlabel and find out the label in cellForRowAtIndexPath as following :

UILabel *myLabel = (UILabel *)[cell.contentView viewWithTag:givenTagValue];

Then apply your required behaviour for this label as you want.

Hope it helps..

在这种情况下,我更喜欢创建自定义子类并根据需要应用布局,因此,如果收到任何自动布局警告,我总是知道这是因为布局不正确。

Actually, changing the frame the textLabel in cell is NOT allowed in TableView. The width of frame for the textlabel of each cell relates to the cell, developer couldn't set it programmatically. However, I worked out a feasible solution: we may not change the frame of the textLabel, but we may change the source text alternatively. If we can truncate the source string in advance, this problem is overcome.

//Assign each element in the array a row in table view

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var myCell = myTableView.dequeueReusableCell(withIdentifier: "The Cell")

    if myCell == nil {
        myCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "The Cell")
    }

    myCell?.accessoryType = UITableViewCellAccessoryType.none

    // If the text is too long (longer than 25 chars), the text will be truncated
    // The lenght of label cannot be set effectively here, so I truncate the source string alternatively
    var myString: String = sectionArray[indexPath.row]
    if myString.characters.count > 25 {

        let myIndex = myString.index(myString.startIndex, offsetBy: 25)
        myString = myString.substring(to: myIndex)
        myString += "..."
    }

    myCell?.textLabel?.text = myString
    myCell?.detailTextLabel?.text = "\(NSDate())"

    return myCell!
}

That supports to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM