【问题标题】:Changing position of a UILabel for a custom UITableVIewCell更改自定义 UITableVIewCell 的 UILabel 的位置
【发布时间】:2015-02-05 12:21:27
【问题描述】:

好的,所以我一直在尝试解决我这几天遇到的问题,但没有成功。今天我找到了一个解决方案,但不是我的问题的完整解决方案。

这就是问题所在。

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

但是在表格第二次重新加载之后,或者当我来回切换选项卡时,它会更改为我希望它从一开始的样子。像这样。

这是在 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 中找到了一个解决方案,但问题是它会针对所有单元格进行更改。如您所见,我只需要更改几个单元格。请帮助我,我该怎么办...

编辑 1 的完整代码 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;
}

【问题讨论】:

  • 试着把它放在 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  • 我已经试过了,求救@soulshined
  • 您是否在描述单元格布局的笔尖或情节提要中使用自动布局?如果是这样设置 gameTimeLabel 框架可能是您的问题 - 您需要调整控制其位置的约束(相对于框架)。我还认为您应该在 GameInfoTableViewCell 本身的 layoutSubviews 中进行布局(如果不使用自动布局!)

标签: ios objective-c uitableview alignment


【解决方案1】:

只需检查 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 实例的数据。

【讨论】:

  • 你误解了我的问题。我提供的代码在我提供的图像中产生。这是正确的,它改变了我需要的单元格,但主要问题是它不是第一次而是第二次改变它。我链接的帖子中的解决方案解决了它第一次改变它但它改变了所有单元格@NewEngland的问题
  • 所以只有在你重新加载 tableView 或从另一个 VC 转到它之后,你才能得到想要的结果?在这种情况下,请发布更多代码。另外,您如何设置整个表格视图
  • 正确。我认为这与我如何设置表格无关,但我会发布编辑。 “在 cellForRowAtIndexPath 返回后,容器似乎正在调整大小,并且在那里的某处为我们的标签找到了新位置;这可以解释为什么稍后重用单元​​格可以解决问题。”从stackoverflow.com/questions/8373214/…得到这个报价
猜你喜欢
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多