【发布时间】:2013-11-06 18:33:39
【问题描述】:
这是一个奇怪的问题。我的故事板上有一个标签,位于自定义原型单元格中,其标签为 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:。
我尝试清理项目和构建文件夹,从设备卸载等。重新运行它。还是没有运气。
【问题讨论】:
-
你是如何得到你的标签的?
-
我不确定你在问什么,ManicMonkOnMac。我将标签从对象库拖到我的单元原型中。我在属性检查器中分配了标签。我正在尝试将字符串分配给 tableView:cellForRowAtIndexPath: 中的标签。字符串数据本身——这里似乎没有问题,因为我可以记录我想要的内容,甚至可以将用户名输入设备上的单元格,只要我不使用 stringWithFormat:——从我的后端服务的服务器。似乎 stringWithFormat: 是问题的根源。
标签: nsstring storyboard uilabel custom-cell stringwithformat