2013-08-26 67 views
0

我想在parse.com上使用字符串来设置UILabel中的文本。在解析我有一个名为“isItemOnSpecial”的字符串列,如果该项目是特殊的,我会将它设置为yes。如果字符串设置为yes,那么我希望UILabel显示来自列specialPrice的字符串othwerwise我想UILabel被隐藏。这里是我的代码:使用“isEqualToString”设置标签文本

// This is where I am setting up my tableViewCell// 

NSString *itemOnSpecial = [object objectForKey:@"isItemOnSpecial"]; 

if ([itemOnSpecial isEqualToString:@"yes"]) { 
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5]; 
    specialLabel.text = [object objectForKey:@"specialPrice"]; 
} else { 
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5]; 
    [specialLabel setHidden:true]; 
} 

当我运行该应用程序使用此代码UILabel总是设置隐藏。 任何与此有关的帮助将非常感激,它一直在困扰着我很长一段时间。

谢谢你的时间。

+1

'[object objectForKey:@“isItemOnSpecial”]''的输出是什么? – Desdenova

+0

yah..check'NSLog(@“%@”,[object objectForKey:@“isItemOnSpecial”]);' – preetam

+0

'if([@“Some String”caseInsensitiveCompare:@“yes”] == NSOrderedSame)'try this – Buntylm

回答

4

我看到的最直接的问题是,它看起来像你使用这个代码的UITableViewCell,或类似的情况下,你需要确保取消隐藏可能已被隐藏的标签,细胞。例如:

NSString *itemOnSpecial = [object objectForKey:@"isItemOnSpecial"]; 

if ([itemOnSpecial caseInsensitiveCompare:@"yes"] == NSOrderedSame) { 
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5]; 
    specialLabel.text = [object objectForKey:@"specialPrice"]; 
    [specialLabel setHidden:NO]; // Notice this line 
}else{ 
    UILabel *specialLabel = (UILabel*) [cell viewWithTag:5]; 
    [specialLabel setHidden:YES]; 
} 
+1

嗯......我认为问题会是大小写敏感的字符。 –

+0

你好,我试过这样做,但即使“isItemOnSpecial”设置为“no”,specialLabel.text现在显示@“specialPrice”。 – Shayno

+0

我有一个大写字母,我不应该有。愚蠢的错误!你的代码完美地工作。非常感谢你! – Shayno

0

试试这个::

if([@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame) { 
    // strings are equal except for possibly case 
} 

OR这样的:

[someString compare:otherString options:NSCaseInsensitiveSearch]; 

希望它帮助!

+0

感谢Armaan,但我似乎无法得到它使用这些方法的工作 – Shayno