Search code examples
iosobjective-cuitableviewframedetailtextlabel

TextLabel shifts up when detailTextLabel becomes multiline


i'm currently making an app where the suer selects an MKMapView annotation and then the title of the annotation(pin) is set to a detailtextLabel in a Right Detail UITableViewCell. My Problem is that when the text is large, the detailTextLabel becomes multiple lines. When this happens the TextLabel of the cell(the one the left) shifts up. Heres What I've tried:

  • In the cellForRowAtIndexPath Method, I tried adjusting the frame through the following code:
    CGRect frame = cell.textLabel.frame;

    frame.origin.y = cell.frame.size.height/2;

    cell.textLabel.frame = frame;

    Where cell is a UITableViewCell that is set to right detail

  • Subclass the cell and tun try to adjust the frame in the -(void)layoutSubviews

How do I stop it from going up and keep it at the center of the cell?


Solution

  • If you want to do a custom layout of UITableViewCell, then you need to add your custom UI elements to its -[UITableViewCell contentView] instead of trying to modify geometry of standard UI elements that are created by default.

    So, in your case, you need to add two UILabels and set their position so that:

    1. Title label will not move at all
    2. Detail text label will be also multiline

    In this way you'll be able to solve this problem!