iOS故事板的UILabel:我可以显式设置文本,但是引入stringWithFormat:无法设置文本

时间:2013-11-06 18:33:40

标签: nsstring storyboard uilabel custom-cell stringwithformat

这很奇怪。我的故事板上有一个自定义原型单元格中的标签,其标签为102.正如标题所示,我可以通过各种方式设置标签文本,并按预期显示在设备上。以下是我尝试过的方法:

nameLabel.text = @"My buddy's name";
nameLabel.text = [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"];
[nameLabel setText:[[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]];

然而,这些不起作用......

NSString *nameString = [NSString stringWithFormat:@"game with %@", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]];
[nameLabel setText:nameString];

...也

[nameLabel setText:[NSString stringWithFormat:@"game with %@", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]]];

和...

nameLabel.text = [NSString stringWithFormat:@"game with %@", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]];

我需要细胞阅读:“某某某游戏”,其中“某某”是对手的名字,显然。关于会导致这种情况的任何想法?

编辑:所以这里有更多的代码,演示了我是如何攻击这个东西的:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MatchCell"];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:102];
    ...
}

if (indexPath.section == 0) {
    nameLabel.text = [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"];
    [nameLabel setText:[[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]];
    NSString *nameString = [NSString stringWithFormat:@"game with %@", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]];
    [nameLabel setText:nameString];
    [nameLabel setText:[NSString stringWithFormat:@"game with %@", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]]];
    nameLabel.text = [NSString stringWithFormat:@"game with %@", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:@"player2Username"]];
}
        return cell;
}

我正在使用Parse作为我的BaaS。那里似乎没有问题,因为我可以在我的iPhone(在单元格中)检索,记录和显示玩家的用户名和我想要的任何其他数据,只要我不包含stringWithFormat:。

我尝试清理项目和构建文件夹,从设备中卸载等。重新运行它。仍然没有运气。

1 个答案:

答案 0 :(得分:0)

纽巴错误。我没有将UILabel的大小放宽到足以容纳额外的文本。也就是说我可以显式设置一个字符串(例如nameLabel.text = @“播放器名称”;),并且有足够的空间来容纳玩家的名字,但每当我尝试将“游戏与”添加到任何用户的名字时,它有可能使整个字符串太长,UILabel的小宽度可以容纳。所以,我想它正在恢复标签的文本(我在Interface Builder / Storyboard中设置),“游戏与......”。

简而言之,请确保您的UILabels足够宽,以容纳他们将要处理的文本,孩子们!如果你不这样做,有人会患病或怀孕或更糟。我已经看过一千次了。