Objective c 根据选中的复选框数量向NsMutableArray添加对象

Objective c 根据选中的复选框数量向NsMutableArray添加对象,objective-c,ios,xcode,uibutton,nsmutablearray,Objective C,Ios,Xcode,Uibutton,Nsmutablearray,我有一个联系人列表,在他们的名字旁边有一个复选框 我想在选中单元格文本时将其存储到数组中,而在未选中单元格文本时将其从数组中删除。我的代码只有在选择1时才能正常工作,但当我选中多个复选框并记录时,它只给我1个值。下面是我的代码 #import <UIKit/UIKit.h> #import "AddressBookViewController.h" @class AddressBookViewController; @interface AddressBookCell : UI

我有一个联系人列表,在他们的名字旁边有一个复选框

我想在选中单元格文本时将其存储到数组中,而在未选中单元格文本时将其从数组中删除。我的代码只有在选择1时才能正常工作,但当我选中多个复选框并记录时,它只给我1个值。下面是我的代码

#import <UIKit/UIKit.h>
#import "AddressBookViewController.h"

@class AddressBookViewController;

@interface AddressBookCell : UITableViewCell {
IBOutlet UIButton *checkbox;
NSMutableArray *array;

}

@property (nonatomic, retain) IBOutlet UIButton *checkbox;
@end
}

编辑
您的
NSMutableArray
被定义为表单元格的一部分,这意味着每个单元格都有自己的数组,这就是为什么在此数组中永远不会有多个项。您需要将此数组声明为您的
UITableView
的成员。

您的应用程序似乎每次都显示整个列表,然后使用所选内容执行任务。是这样吗?您选择/取消选择人员,然后它存储他们,并发送文本?那么您想保存上一个选择以便用户下次调出联系人选择吗

在我看来,您应该使用
NSMutableSet
而不是数组来执行此任务。看起来顺序并不重要,一套更合适。我将为集合声明一个属性,如
@property(非原子,保留)NSMutableSet*contactList
然后
@合成联系人列表


然后,当用户点击“完成”或在做出选择时点击的任何内容时,您会将所选选项存储在NSSet中。使用
[self.contactList setSet:myNonMutableSet]将此NSSet复制到您的NSMutableSet。然后每次打开列表时,您只需通过调用
self.contactList
检查上次选择的内容,查看最后一个状态。

我是否应该将NSSet放在我的TableView类或tableViewController类中?它应该放在您的controller类(或您定义为数据源的任何类)中?单击按钮时,我会得到一个断点?,有什么问题吗?只是开玩笑,xcode太蠢了,我只是删除了代码并重新编写了它,它仍然有效。它仍然没有将单元格添加到ab.controller.savedPeople
#import "AddressBookCell.h"

@implementation AddressBookCell
@synthesize checkbox;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [self.checkbox setFrame:checkboxRect];  
    [self.checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
    [self.checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];
    [self.checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.accessoryView = self.checkbox;
    array = [[NSMutableArray alloc]init];

}
return self;
-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (UITableViewCell *)sender.superview;
if(sender.selected){
    [array addObject:cell.textLabel.text];
}else{
    if([array containsObject:cell.textLabel.text]){
        [array removeObject:cell.textLabel.text];
        NSLog(@"it got removed");
    }
}
NSLog(@"%@",array);
}


-(void)dealloc{
[super dealloc];
}
-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
if(sender.selected){
    [abController.savedPeople addObject:cell.textLabel.text];
}else{
    if([abController.savedPeople containsObject:cell]){
        [abController.savedPeople removeObject:cell];
    }
}

}