I'm having problems in the iPad view, I'm trying to make a table in which each cell will consist of a textLabel
and a button UIButton
, when I try it on an iPhone the view looks fine, but when I try it with an iPad it looks like this :
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 suggestion what it could be?
PS: I'm pretty new with objective-c development
Well, it is the typical example of autolayout. You have to define its 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 you have to place the image yourself. To do that by hand you have to:
First thing, you don't need to position the button. If you do, it will draw it for you in the coordinates that you indicate. Create the active center with size only, without position
You have to set translatesAutoresizingMaskIntoConstraints to NO to be able to set constraints by hand
Then you have to add activeCenter to the view (the cell in this case)
Finally you 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 indicating that it is at x points from the left margin (for example 15) and that it center it with respect to the cell would be enough.
So the lines where you test the orientation and if it's an iPad I would remove them and only leave the frame in the Active center. All this was: int orientation = [[UIApplication sharedApplication]statusBarOrientation];
and I would just leave the
The TextLabel overlaps
Well, in this case you 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
And then, as with the activecenter, add the constraints. In this case we are going to give it one to the left of about 15 points, we are going to center it with respect to the cell and we are going to tell it that it has to be another 15 points away from the activecenter.
I hope it helps you, I already have forgotten ObjC a little
This is due to not using or using incorrectly
AutoLayout
. You must create some constraints that make the checkbox fixed to the right. Then, the right constraint of theUILabel
should always be "glued" to the left side of the checkbox.It's complicated to explain around here, but basically it's a problem of
AutoLayout