我想根据 boolean 值 true x false 来设置 ImageView 是否隐藏,它位于 Parse 数据库中。你能给我一些想法吗?由于我的 CustomCell.h 中有属性 ImageView 而出现问题
编辑:
PFObject *yes = [PFObject objectWithClassName:@"MyClass"];
cell.discounts.hidden = [yes[@"yesnocolumn"] boolValue];
我只是不明白 PFObject 的用法,myclass 返回(空)值。
编辑2:
PFQuery *query = [PFQuery queryWithClassName:@"Classname"];
[query whereKey:@"yesnocolumn" equalTo:@1];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) {
cell.discounts.hidden = YES;
}else{
cell.discounts.hidden = NO;
}}];
最佳答案
服务中的 boolean 值看起来像@0 或@1,因此您不能只测试真实性,因为:
if (@0) {
// this condition is true. that's surprising!
}
所以安全的测试方法是:
cell.imageView.hidden = [myParseObject[@"hidden"] boolValue];
编辑 - 现在我明白了误解。以下是解析的工作原理:
1) 在本地创建一个对象:
PFObject *yes = [PFObject objectWithClassName:@"MyClass"];
此对象不会初始化任何值。因此它包含的任何 boolean 值都将为 0 或 NO。
2) 在本地设置一些值:
yes[@"yesnocolumn"] = @1; // makes it true
// or = [NSNumber numberWithBool:YES];
3) 保存。 (您也可以使用数据浏览器手动初始化对象)。
[yes saveInBackground];
4) 获取对象。我认为这是给你带来麻烦的部分......仅仅创建一个本地对象并没有多大作用。 parse 的思想是将对象存储在云端。要获取它们,您需要一个 PFQuery。
PFQuery *query = [PFQuery queryWithClassName:@"MyClass"];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// this will return all (up to 100 by default, MyClass objects that are
// saved in the cloud
if (!error && results.count) {
NSLog(@"the first object is %@", results[0]);
NSLog(@"the bool is %d", [results[0][@"yesnocolumn"] boolValue];
}
}];
这有意义吗?在您在本地设置它们或检索已初始化的远程副本之前,您不能期望该对象具有任何初始化的值。另请注意,如果您在 saveInBackground 之后的下一个源代码行上运行查询,您将得不到保存的结果,因为保存尚未完成。开始在您的应用程序运行一次时保存,然后在数据浏览器中查看,然后进行查询。
值得对 their docs here 进行全面审查.
关于ios - 根据 boolean 值解析自定义单元格显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004433/