Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

ios - Changing position of a UILabel for a custom UITableVIewCell

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...