我现在不在我的Xcode计算机上,所以我不能测试这个,但是好像你并不需要另一个数组控制器,而只是另一个数组来存放喜欢的水果。我认为你需要从你的acquiredFruits数组中创建一个字典数组,这个数组将有一个水果名的关键字,另一个关键字的布尔值是否被选中 - 这个布尔值将被绑定到你的第二列。我不知道如何告诉likesFruit数组需要添加一个新的水果的下一步 - 我认为复选框可以有一个操作方法,您可以使用这个方法让favoriteFruit数组将对象添加到行点击复选框。
编辑完成后:
这里是一个如何做我建议的例子。我获取了一系列水果并将其转换为一个包含复选框值的键的字典数组(字符串数据)(在IB中,数组控制器的内容数组绑定到了数据,并且列绑定到了Array Controller.arrangedObjects.fruitName和Array Controller.arrangedObjects.isLiked)。 checkChanged是一个连接到复选框的IBAction(但注意发件人实际上是表视图),并且我使用复选框的值来决定是否向fruitsFruits添加一个水果或删除一个水果。我把一个更多的方法,连接到一个按钮,只是为了检查在likesFruits中的值。
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.theData = [NSMutableArray array];
self.likedFruit =[NSMutableArray array];
NSArray *acquiredFruits = @[@"Apple",@"Orange",@"Pear",@"Peach"];
for (NSString *aFruit in acquiredFruits) {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:aFruit,@"fruitName",[NSNumber numberWithBool:NO],@"isLiked", nil];
[self.theData addObject:[dict mutableCopy]];
}
self.theData = _theData;
// NSLog(@"%@",self.theData);
}
-(IBAction)checkChanged:(NSTableView *)sender { //connected to the button cell in the table view (but sender is the table view)
NSString *theFruit = [[self.controller.arrangedObjects objectAtIndex:sender.clickedRow ] valueForKey:@"fruitName"];
BOOL doWeLikeIt = [[[self.controller.arrangedObjects objectAtIndex:sender.clickedRow] valueForKey:@"isLiked"] boolValue];
if (doWeLikeIt) {
[self.likedFruit addObject:theFruit];
}else{
[self.likedFruit removeObject:theFruit];
}
}
-(IBAction)logLikedFruits:(id)sender {
NSLog(@"%@",self.likedFruit);
}
你在另一个表显示这些likedFruits,或只需要他们在一个数组? – rdelmar 2012-07-30 03:11:27
我希望我的acquireFruit绑定表中的行的已检查状态代表水果在likesFruits中的存在 - 我不需要另一个tableVlew,但我正计划在应用程序中的其他地方遍历喜欢的水果。 – arbales 2012-07-30 10:34:21