uitableviewcell does not fit well on the ipad

1

I'm having problems in the iPad view, I'm trying to make a table where each cell will be made up of textLabel and a button UIButton , when I try it on an iphone the view goes well, but when I I try with an iPad it comes out in the following way:

The code I have is the following:

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

    NSString *simpleTable = [NSString stringWithFormat:@"simpleTableIdentifier%ld", (long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTable];

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

    Centro_DTO *cen_datos = [[Centro_DTO alloc] init];
    cen_datos = [self.computersCentros objectAtIndex:indexPath.row];

    NSString *currentL = ([Global sharedMySingleton].test);

    if ([currentL isEqualToString:@"es"]) {

        cell.textLabel.text = cen_datos.nombreES;
    } else if ([currentL isEqualToString:@"ca-ES"]) {

        cell.textLabel.text = cen_datos.nombreCA;
    } else if ([currentL isEqualToString:@"en"]) {

        cell.textLabel.text = cen_datos.nombreEN;
    }

    cell.textLabel.textColor = [UIColor colorWithRed:(0/255.0) green:(44/255.0) blue:(82/255.0) alpha:1];
    cell.textLabel.font = [UIFont systemFontOfSize:13.0];
    cell.accessoryType = UITableViewCellAccessoryNone;

    UIButton *centroActivo = [[UIButton alloc] init];
    centroActivo.highlighted = NO;

    int orientation = [[UIApplication sharedApplication]statusBarOrientation];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad || (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && orientation != UIDeviceOrientationPortrait)) {
        centroActivo.frame = CGRectMake(350.0, 15.0f, 20.0f, 20.0f);
    } else {
        centroActivo.frame = CGRectMake(290.0, 15.0f, 20.0f, 20.0f);
    }
    centroActivo.tag = indexPath.row;
    [centroActivo setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateSelected];
    [centroActivo setImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState:UIControlStateNormal];
    [centroActivo addTarget:self action:@selector(checkboxSelected:) forControlEvents:UIControlEventTouchUpInside];

    if (cen_datos.activo == 1) {

        [centroActivo setSelected:YES];
    } else {

        [centroActivo setSelected:NO];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView addSubview:centroActivo];

    return cell;
}

and what I want is for it to look like this on the ipad:

Any suggestions on what it can be?

PS: I am quite new with the development in

    
asked by Joacer 23.01.2017 в 16:53
source

2 answers

2

Well, it's the typical example of autolayout. You have to define your position with respect to the rest of the items. You can do it using a .xib or by hand. The textlabel is positioned correctly by the cell by default, but the image must be placed by you. To do that by hand you have to:

  • First thing, you do not need to position the button. If you do, I will draw it in the coordinates that you indicate. Create the centroactivo only with size, without position

    centroActivo.frame = CGRectMake(0.0, 0.0, 20.0f, 20.0f);
    
  • You have to put translatesAutoresizingMaskIntoConstraints to NO to be able to put constraints at hand

    centroActivo.translatesAutoresizingMaskIntoConstraints = NO;
    
  • Then you have to add centroActivo to the view (the cell in this case)

  • Finally add constraints to indicate where to position it. Here is the butter. You have to give the necessary positions so that iOS knows where to draw. In this case, I think that by indicating that it is at x points on the left margin (for example 15) and that it is centered on the cell, it would be enough.

    NSlayoutConstraint *trailing = [NSLayoutContraint 
                                 constraintWithItem: centroActivo
                                 attribute:NSLayoutAttributeTrailing
                                 relatedBy: NSLayoutRelationEqual
                                 toItem: self.contentView
                                 attribute: NSLayoutAttributeTrailing
                                 multiplier: 1.0f
                                 constant: 15.f];
    NSlayoutConstraint *center = [NSLayoutConstraint constraintWithItem:centroActivo attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual                                                             
                                    toItem:self.contentView
                                 attribute:NSLayoutAttributeCenterX 
                                multiplier:1 
                                  constant:0];
    
    
    [self.contentView addConstraint:trailing];
    [self.contentView addConstraint:center];
    

So the lines where with orientation tests and if it's iPad would remove them and only leave the frame of the centerActive All this outside: int orientation = [[UIApplication sharedApplication] statusBarOrientation];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad || (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && orientation != UIDeviceOrientationPortrait)) {
    centroActivo.frame = CGRectMake(350.0, 15.0f, 20.0f, 20.0f);
} else {
    centroActivo.frame = CGRectMake(290.0, 15.0f, 20.0f, 20.0f);
}

and would only leave the

centroActivo.frame = CGRectMake(0.0, .0f, 20.0f, 20.0f);

TextLabel overlaps

Well, for this case we have to do something similar. Tell him where he has to paint it So the first thing is to tell him that you are going to touch the constraints

    cell.textLabel.translatesAutoresizingMaskIntoConstraints = NO;

And then as with the centerActive add the constraints. In this case we are going to give him a left one of about 15 points, we will center him with respect to the cell and we will tell him that he has to be another 15 points away from the centerActivo.

    //esto es la derecha, así que se relaciona con centroActivo
    NSlayoutConstraint *trailingText = [NSLayoutContraint 
                         constraintWithItem: cell.textLabel
                         attribute:NSLayoutAttributeTrailing
                         relatedBy: NSLayoutRelationEqual
                         toItem: centroActivo
                         attribute: NSLayoutAttributeTrailing
                         multiplier: 1.0f
                         constant: 15.f];
    NSlayoutConstraint *centerText = [NSLayoutConstraint     constraintWithItem:cell.textLabel
                         attribute:NSLayoutAttributeCenterX
                         relatedBy:NSLayoutRelationEqual                                                             
                            toItem:self.contentView
                         attribute:NSLayoutAttributeCenterX 
                        multiplier:1 
                          constant:0];
    //esto es con el margen izquierdo
    NSlayoutConstraint *leadingText = [NSLayoutContraint 
                         constraintWithItem: cell.textLabel
                         attribute:NSLayoutAttributeLeading
                         relatedBy: NSLayoutRelationEqual
                         toItem: self.contentView
                         attribute: NSLayoutAttributeTrailing
                         multiplier: 1.0f
                         constant: 15.f];

    [self.contentView addConstraint:leadingText];
    [self.contentView addConstraint:trailingText];
    [self.contentView addConstraint:centerText];

I hope it serves you, the ObjC I already have it a little forgotten

    
answered by 23.01.2017 / 18:52
source
1

This is due to not using or incorrectly using AutoLayout . You must create some constraints that leave the checkbox to the right. Then, the right constraint of the UILabel should always be "stuck" to the left side of the checkbox.

It's complicated to explain here, but basically it's a problem of AutoLayout

    
answered by 23.01.2017 в 18:52