更改UILabel的位置以用于自定义UITableVIewCell

时间:2014-12-07 21:17:18

标签: ios objective-c uitableview alignment

好的,所以我一直试图修复一个问题,我现在已经有几天没有成功了。今天我找到了解决方案,但没有找到解决我问题的全面解决方案。

所以这就是问题所在。

从这样开始,请注意时间标签的对齐方式(左侧的标签)

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的解决方案,但问题是它改变了所有的细胞。正如你所看到的,我只需要改变一些细胞。请帮助我,我能从创意中做些什么......

编辑 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;
}

1 个答案:

答案 0 :(得分: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. 中重置布局
  3. 最初加载视图后重新加载UITableView实例的数据。