【发布时间】:2015-08-30 20:38:42
【问题描述】:
我有一个 NSArray (self.allUsers),里面有 PFUser 对象:
"<PFUser: 0x7ff578f501f0, objectId: qvFwzbBGky, localId: (null)> {\n friends = \"<PFRelation: 0x7ff578f4eef0, 0x0.(null) -> _User>\";\n phone = 0;\n surname = test;\n username = \"test@gmail.com\";\n}"
我可以通过[self.allUsers valueForKey:@"phone"]访问电话号码
我还有另一个NSArray (self.sectionedPersonName),里面有NSObject Person(按部分排序)。我可以使用person.number 访问电话号码。
我想比较这两个数组之间的电话号码。并记录与PFUser objectId ([self.allUsers valueForKey:@"objectId"];) 匹配的时间
也许我必须使用 NSPredicate CONTAINS ?
我不知道 NSObject 和 PFUser 是否可以。
之后,我想更改匹配的单元格颜色。
在 didSelect 中,当我单击它时,只需记录来自 PFUser 的 objectId。我们要在PFUser和NSObject之间建立一个对应关系...
我成功显示了匹配的 PFUser 的 objectId:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
cell.detailTextLabel.text = person.number;
if ( [[self.allUsers valueForKey:@"phone"] containsObject:person.number] ) {
// do found
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
__block PFUser *foundedPFUser;
NSMutableArray *realResult = [NSMutableArray new];
[self.tableData enumerateObjectsUsingBlock:^(Person *obj, NSUInteger idx, BOOL *stop) {
NSArray *result = [self.allUsers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K = %@", @"phone", obj.number]];
if(result.count){
[realResult addObjectsFromArray:result];
}
}];
NSLog(@"%@",realResult);
if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
NSLog(@"%@", person.number);
}
else{
}
现在,当我点击一个单元格时,该日志:
- 我点击的人的号码。
- 与号码匹配的三个PFUser
日志:
2015-08-31 12:02:59.727 ChillN[691:18988] (
"<PFUser: 0x7f992a4df7e0, objectId: bEO3D62Cvo, localId: (null)> {\n friends = \"<PFRelation: 0x7f992a4a3b60, 0x0.(null) -> _User>\";\n phone = 5555228243;\n surname = test4;\n username = \"test4@gmail.com\";\n}",
"<PFUser: 0x7f992a4a3d30, objectId: rlG21mKFJZ, localId: (null)> {\n friends = \"<PFRelation: 0x7f992a4e08d0, 0x0.(null) -> _User>\";\n phone = 31560987;\n surname = test2;\n username = \"test2@gmail.com\";\n}",
"<PFUser: 0x7f992a47af90, objectId: 7X0OVanRZ9, localId: (null)> {\n friends = \"<PFRelation: 0x7f992a4e0970, 0x0.(null) -> _User>\";\n phone = 7075551854;\n surname = test3;\n username = \"test3@gmail.com\";\n}"
)
2015-08-31 12:02:59.727 ChillN[691:18988] 5555228243
现在我只希望匹配好的PFUser。当我点击“Anna Haro”时,发现person.number为5555228243,在PFUser中找到“5555228243”,记录PFUser的objectId。
我的期待:
日志:
2015-08-31 12:02:59.727 ChillN[691:18988] (
"<PFUser: 0x7f992a4df7e0, objectId: bEO3D62Cvo, localId: (null)> {\n friends = \"<PFRelation: 0x7f992a4a3b60, 0x0.(null) -> _User>\";\n phone = 5555228243;\n surname = test4;\n username = \"test4@gmail.com\";\n}"
)
2015-08-31 12:02:59.727 ChillN[691:18988] 5555228243
【问题讨论】:
标签: ios objective-c parse-platform nsarray nspredicate