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

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

所以这就是问题所在。

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

截图1

但是在第二次重新加载表格之后,或者当我来回切换标签时,它会从一开始就改变为我想要的样子。 喜欢这个。

截图2

这是在cellForRowAtIndexPath:中执行此操作的代码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; 

我在这篇文章中找到了一个解决方案。 在自定义UITableViewCell中更改自定义UIButton的位置,但问题是它改变了所有的细胞。 正如你所看到的,我只需要改变一些细胞。 请帮助我,我能从创意中做些什么…

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

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