2014-12-07 96 views
0

好吧,所以我一直在努力解决一个问题,我得到了几天没有成功。今天,我发现了一个解决方案,但没有解决我的问题。更改自定义UITableVIewCell的UILabel的位置

所以这是问题所在。

它开始这样的,请注意时间标签(那些向左)对齐

Screenshot 1

但第二次表重装后或当我切换标签它来回变化,然后变成我想从一开始就看起来的样子。喜欢这个。

Screenshot 2

这是做这里面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; 

我发现这个职位Changing the position of custom UIButton in custom UITableViewCell的解决方案的代码,但问题是,它改变了所有单元。正如你所看到的,我只需要它改变几个单元格。请帮我我该怎么做IM的想法......

EDIT 1cellForRowAtIndexPath

- (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; 
} 
+0

尝试把它放在 - (空)的tableView:(UITableView的*)的tableView willDisplayCell:(*的UITableViewCell)单元forRowAtIndexPath:(NSIndexPath *)indexPath { – soulshined 2014-12-07 21:42:12

+0

我已经尝试过了,Dst的帮助,您使用的@soulshined – 2014-12-07 22:03:52

+0

在描述单元格布局的笔尖或故事板中自动布局?如果这样设置gameTimeLabel框架可能是您的问题 - 您需要调整控制其位置的约束(vs框架)。我也认为你应该在GameInfoTableViewCell本身的layoutSubviews(如果不使用自动布局!) – TomSwift 2014-12-09 16:37:08

回答

0

整个代码只是检查了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;} 

编辑 - -

它仍然没有100%清楚究竟是什么问题,但继承人可以尝试几件事情。

  1. 复位的布局在layoutSubviews [self.view setNeedsLayout];
  2. 视图负载最初后重新装入的UITableView实例的数据。
+0

你做错了我的问题。我提供的图片中提供的代码结果。这是正确的,它改变了我需要的细胞,但主要的问题是,它不会改变它的第一次,但第二次。 从我链接的帖子的解决方案解决了它第一次改变它的问题,但它改变了所有的单元格@NewEngland – 2014-12-07 21:55:40

+0

所以只有当你重新加载tableView或从另一个VC去它,你会得到所需的结果?在这种情况下,请张贴更多的代码。另外,你如何设置整个表格视图 – MendyK 2014-12-07 21:59:24

+0

正确。 我不认为这与我如何设置表,但生病发布编辑。 “ 好像容器正在被cellForRowAtIndexPath返回后调整大小,并且在那里找到了我们标签的新位置;这可以解释为什么重复使用单元后来解决了问题。 从http://stackoverflow.com/questions/8373214/uilabel-position-in-a-uitableviewcell-fails-on-the-first-try – 2014-12-07 22:04:55

相关问题