2014-12-07 100 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

相關問題