简体   繁体   中英

Constraints not working when using viewWithTag to get labels inside UITableviewCell

I really can't get my head around why this isn't working properly. I have a TableViewController with dynamic prototypes. I put 4 labels in a prototype named "InfoCell", and gave them constraints. If i run the app with the following cellForRowAtIndexPath:

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

    return cell;
}

what I get is this. Everything looks fine 'til now

I did this just to check that the labels were being displayed in the right place. The page is supposed to show 2 cells, so everything looks fine.

Now, the problems start when I try to get a reference to the labels in order to change the text. Even without actually changing the text, if my code looks like this:

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

    UILabel *nameLabel = (UILabel *)[cell viewWithTag:10];
    UILabel *surnameLabel = (UILabel *)[cell viewWithTag:20];
    UILabel *roleLabel = (UILabel *)[cell viewWithTag:30];
    UILabel *spouseNameLabel = (UILabel *)[cell viewWithTag:40];

    [cell addSubview:nameLabel];
    [cell addSubview:surnameLabel];
    [cell addSubview:roleLabel];
    [cell addSubview:spouseNameLabel];

    return cell;
}

I get this. Labels' positions went nuts

I tried, for example, to change the frame of each label programmatically,

nameLabel.frame = CGRectMake(15.0, 50.0, 120.0, 20.0)

but it just doesn't do anything, I suppose because auto-layout is enabled... but I'm too far into the project to disable auto-layout. Plus I've seen the use of viewWithTag as I wrote above working without the need to re-locate the labels programmatically, so it bugs me not to know what's really happening there!

Remember that when you have constraints added on any of the UI object, you can't change the frame of that object by changing its CGRect . In fact, you should change its constraint value. Now the problem in your code is ,

[cell addSubview:nameLabel];
[cell addSubview:surnameLabel];
[cell addSubview:roleLabel];
[cell addSubview:spouseNameLabel];

above 4 lines. When you have added a UILabel in storyboard, why are you adding them again by using addSubview method ? Remove above 4 lines, and to set text on your UILabel , you already have a reference which you are accessing by using their tag values. So your method should looks like as below.

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

  UILabel *nameLabel = (UILabel *)[cell viewWithTag:10];
  UILabel *surnameLabel = (UILabel *)[cell viewWithTag:20];
  UILabel *roleLabel = (UILabel *)[cell viewWithTag:30];
  UILabel *spouseNameLabel = (UILabel *)[cell viewWithTag:40];

  nameLabel.text = @"";
  surnameLabel.text = @"";
  roleLabel.text = @"";
  spouseNameLabel.text = @"";

  return cell;
}

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