我正在尝试在数组中找到选定的人。我正确地得到了没有人被选中的地方,但是,如果选择了组中的一个人,则选择了组中的所有人。
经过长时间的摸索后,我可以使用一些帮助来查看是否有明显的东西。
这个动作是cellForRowAtIndexPath
这样进行的:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s", __FUNCTION__);
static NSString *pCell = @"Cell";
PeopleCell *cell = (PeopleCell *)[aTableView dequeueReusableCellWithIdentifier:pCell];
if (cell == nil) {
cell = [[PeopleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pCell];
}
people = [peopleArray objectAtIndex:[indexPath row]];
NSString *text = [people objectForKey:@"name"];
cell.textLabel.text = text;
if (selectedPeopleinGroup.count == 0) {
//no people selected in this group
NSLog(@"none");
cell.isSelected = [selectedPeopleinGroup containsObject:text] == NO;
} else {
//there are some people in this group - find out who they are
NSLog(@"some");
NSString *key1 = [selectedPeopleinGroup valueForKey:@"personKey"];
NSString *key2 = [people valueForKey:@"personKey"];
NSLog (@"key1 %@", key1 );
NSLog (@"key2 %@", key2 );
if (key1 == key2) {
cell.isSelected = [selectedPeople containsObject:text] == YES;
} else {
cell.isSelected = [selectedPeople containsObject:text] == NO;
}
}
return cell;
}
该单元格是一个子类 UITableViewCell,如果选中,则在单元格左侧有一个复选标记图像,如果未选中,则另一个图像。非常感谢。