Changing position of a UILabel for a custom UITableVIewCell
Asked Answered
K

1

0

Ok so i have been trying to fix a problem that i got for a couple of days now without success. Today i found a solution but not a FULL solution to my problem.

So this is the problem.

It starts like this, please note the alignment of the time labels(the ones to the left)

Screenshot 1

But after the table reloads for the second time OR when i switch tabs back and forth it THEN changes to what i want it to look like from the beginning. Like this.

Screenshot 2

This is the code that does this inside cellForRowAtIndexPath:

    if ([gameInfoObject.GameTime  isEqual: @"FT"] || ([gameInfoObject.GameTime rangeOfString:@":"].location != NSNotFound)) {   // CHeck to see if its FT or string contains ":" then hide liveB

        cell.liveButton.hidden = YES;
        CGRect frame = cell.gameTimeLabel.frame;
        frame.origin.x= 27;                         // move the label 10pts to the left since no image will be present
        cell.gameTimeLabel.frame= frame;

I found a solution from this post Changing the position of custom UIButton in custom UITableViewCell but the problem is that it changes FOR ALL THE CELLS. As you can see i only need it to change for a few cells. Please help me what can i do im out of ideas...

EDIT 1 the whole code for cellForRowAtIndexPath

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

    static NSString *identifier = @"Cell";
    GamesInfoTableViewCell *cell = (GamesInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

    // Configure the cell...

    GameInfo *gameInfoObject;

    gameInfoObject =[gamesInfoArray objectAtIndex:indexPath.row];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.backgroundColor = TABLECOLOR;

    cell.homeTeamLabel.textColor = TEXT;
    cell.awayTeamLabel.textColor = TEXT;
    cell.gameTimeLabel.textColor = TEXT;


    cell.homeTeamLabel.text = gameInfoObject.HomeTeam;
    cell.awayTeamLabel.text = gameInfoObject.AwayTeam;
    cell.homeTeamScoreLabel.text = gameInfoObject.HomeScore;
    cell.awayTeamScoreLabel.text = gameInfoObject.AwayScore;
    cell.liveButton.image = [UIImage imageNamed:@"1675447.png"]; //Load the green image

    if ([gameInfoObject.GameTime  isEqual: @"FT"] || ([gameInfoObject.GameTime rangeOfString:@":"].location != NSNotFound)) {   // CHeck to see if its FT or string contains ":" then hide liveB

        cell.liveButton.hidden = YES;

        CGRect frame = cell.gameTimeLabel.frame;
        frame.origin.x= 27;                         // move the label 10pts to the left since no image will be present
        cell.gameTimeLabel.frame= frame;


    }

    else
        cell.liveButton.hidden = NO;

    if (([gameInfoObject.GameTime rangeOfString:@":"].location != NSNotFound)) {
        cell.accessoryType = FALSE;
        cell.userInteractionEnabled = NO;
        cell.homeTeamScoreLabel.hidden = YES;
        cell.awayTeamScoreLabel.hidden = YES;
    }

    cell.gameTimeLabel.text = gameInfoObject.GameTime;

    return cell;
}
Kropp answered 7/12, 2014 at 21:17 Comment(3)
Try putting it in -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {Housebreaker
I have already tried it, Dst help @HousebreakerKropp
Are you using auto-layout in the nib or storyboard that describes your cell layout? If so setting the gameTimeLabel frame is likely your issue - you would need to adjust the constraint (vs the frame) that controls its position. I also think you should do your layout in the GameInfoTableViewCell itself, in layoutSubviews (if not using auto layout!)Upturn
I
0

Just check for the indexpath.row

if (indexpath.row == 2){
    cell.liveButton.hidden = YES;
    CGRect frame = cell.gameTimeLabel.frame;
    frame.origin.x= 27;                         // move the label 10pts to the left since no image will be present
    cell.gameTimeLabel.frame= frame;}

EDIT ---

Its still not 100% clear what exactly the problem is but heres a couple of things you could try.

  1. Reset the layout in layoutSubviews [self.view setNeedsLayout];
  2. Reload the data of the UITableView instance after the view loads initially.
Inbreeding answered 7/12, 2014 at 21:51 Comment(3)
You misunderstood my question. The code that i provided with results in the images i provided. Which is correct it changes for the cells that i need but the MAIN problem there is that it does not change it the first time but the second time. The solution from the post that i linked solves the problem where it changes it the first time but it changes for ALL the cells @NewEnglandKropp
So only after you reload the tableView or go to it from another VC do you get the desired result? In that case please post some more code. Also, how are you setting up the entire table viewInbreeding
Correct. I dont think it has to do with how i set up the table but ill post an edit. " It seems like the container is being resized after cellForRowAtIndexPath returns, and that somewhere in there a new position is found for our label; this would explain why reusing the cell later fixes the problem." GOt this quote from #8373714Kropp

© 2022 - 2024 — McMap. All rights reserved.