人の連絡先リストがあり、名前のすぐ横にチェックボックスがあります。
1 http://i.minus.com/jyLvkUt7wnxYs.png
選択されたときにセルテキストを配列に保存し、選択されていないときに配列から削除します。私のコードは 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
.m ファイル
#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];
}
}
}