【问题标题】:Assigning label text shows only format string分配标签文本仅显示格式字符串
【发布时间】:2013-08-04 23:35:53
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"MyCell2";
    int size = [wordListV2 count];
    NSLog(@"there are %d objects in the array", size);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];



    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    // NSString *str =@"%d", [wordListV2[i] ];
    // cell.textLabel.text = str;

    cell.textLabel.text = @"%d", [wordListV2 count];//[[wordListV2 objectAtIndex:indexPath.row] word];
    NSLog(@"Helloo NS LOOOGG %d", [wordListV2 count]);
    return cell;
}

问题是它为“Helloo NS LOOOGG 673”写入“NSLog 部分”673 次,但在单元格中我只能看到 %d 而不是 673。如果我能解决这个问题,我会尝试“[[wordListV2 objectAtIndex:indexPath .row] word]; 而不是数字,但是当我尝试将其放入单元格时它不起作用。”此外,我尝试先将其放入字符串中,然后尝试但仍然无法正常工作:(

好的,伙计们现在可以工作了,我还有另一个问题

好吧,现在它可以工作了,但它仍然不是我真正想做的。我遇到异常并在 0x10e909b 处停止断点可能是什么问题:movl 8(%edx), %edi 我可以收到我的数组的计数,但我无法到达其中的对象这是代码:

 Words * word = [wordListV2 objectAtIndex:0];
 NSLog(@"Helloo NS LOOOGG %@",word.word);
 cell.textLabel.text = word.word;

当我尝试访问 NSMutable 数组时发生异常。此外,我不知道与 ot 有什么关系,但它也给出了这个

"; }; 层 = >", "; }; 层 = >" ) 2013-08-04 18:23:54.720

顺便说一句,Words 是我的班级,而 word 是 NSSTring * 类型。最后

【问题讨论】:

    标签: objective-c uitableview nsmutablearray string-formatting


    【解决方案1】:

    您会看到%d,因为这是您分配给文本标签的字符串,字符串文字之后的所有内容都被忽略(我很惊讶它甚至可以编译):

    cell.textLabel.text = @"%d", [wordListV2 count];
    

    您要做的是使用理解和处理格式字符串的方法创建一个新的 NSString:

    cell.textLabel.text = [NSString stringWithFormat:@"%d", [wordListV2 count]];
    

    NSLog() 起作用的原因是它可以处理格式字符串。但是,ObjC 属性处理格式字符串。

    【讨论】:

    • Doh,错字,应该是stringWithFormat:(大写'F')我会修正答案,
    • 好吧,现在它可以工作了,但它仍然是我真正想做的。可能是什么问题我遇到了异常并且断点停止在 0x10e909b: movl 8(%edx), %edi 我可以收到我的数组的计数,但我无法到达其中的对象这是代码:Words * word = [ wordListV2 objectAtIndex:0]; NSLog(@"Helloo NS LOOOGG %@",word.word);
    • @albatross 这听起来像是一个不同的问题。请发布一个包含崩溃详细信息的新问题(stack trace 和异常名称最有用,它崩溃的汇编代码没有太大帮助)。
    • 好吧,它会在 15 分钟内到来 :)
    猜你喜欢
    • 2013-01-29
    • 2011-07-12
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多