1

複数のボタンを持つカスタム UITableViewCell があります。ボタンが選択された状態か選択されていない状態かを記憶し、それをカスタム コア データ モデル クラスのプロパティに保存したいと思います。複数のカスタム UITableViewCells があり、それぞれに異なる数のボタンがあります。

ボタンには、文字列として巧妙に名前が付けられています: 1,2,3...

このプロジェクトを説明するには、ある教師が、生徒が読んだ本のリストの章数を追跡したいと考えていると想像してください。目標は、各生徒が読んだ章の総数を追跡することです。各本は UITableViewCell です。各本には固有の数の章があります。教師 (または生徒) は、各章を読むときにボタンを選択します。読み取った章はプロパティとして保存されるため、次に UITableViewCell が表示されたときにそのように表示できます。

#import "Student.h"

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    chaptersInBook = 16;
    self.dickensArray = [NSMutableArray array];

    // Book title
    UILabel *bookLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 30)];
    bookLabel.text = @"David Copperfield";

    for (NSInteger index = 0; index < chaptersInBook; index++) // for loop runs 16 times
    {
        // Need to make correct number of buttons based on the chapters in each book
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
        button.tag = index;
        //buttons in rows of seven
        button.frame = CGRectMake(40*(index%7) + 20,40 * (index/7) + 40, 30, 30);
        [button setTitle:[NSString stringWithFormat:@"%d", index+1] forState:UIControlStateNormal];

        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",index+1]] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@"%d", index+1] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(toggleOnOff:) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

        [self.contentView addSubview:button];
        [self.contentView addSubview:bookLabel];
    }
}
return self;
}


-(IBAction)toggleOnOff:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected; // In storyboard the default image and selected image are set
}


-(IBAction)buttonPressed:(id)sender {
    UIButton* button = (UIButton *)sender;
    if (button.selected) {
      int chapter = button.tag + 1;
      NSString *nameOfButton = [NSString stringWithFormat:@"%d",chapter];
      NSString *buttonIsSelected = @"YES";


//Now I want to set student.ch1 to yes but I want to set the '1' to 'chapter'
//Not sure how to do this: append the name of a property with a variable.


}

それで、私の質問は、選択したチャプターの学生プロパティにボタンの状態を保存する最善の方法は? 「student.ch[ここに章番号を追加]」に章番号を追加できればいいのですが、それは不可能だと思います。

//eg.
student.ch1 = [NSNumber numberWithBool:YES];//but replace '1' with the value in the int variable 'chapter'

前もって感謝します。私は間違った木を吠えていると思います。

カート

4

2 に答える 2

1

ボタンの数が少ないため (コメントで 28 と指定しました)、2 のべき乗をボタンのタグとして使用し、整数ビットマスクを使用して 28 個のボタンすべての状態を単一の整数フィールドに格納できます。

4 つのボタンがあるこの例を考えてみましょう (あまり変更せずに 32 に拡張できます)。次のようにボタンにタグを付けます。

button1.tag = 0x01; // Binary 0001
button2.tag = 0x02; // Binary 0010
button3.tag = 0x04; // Binary 0100
button4.tag = 0x08; // Binary 1000

ボタンが選択されるbitwise-ORと、現在の状態のタグ:

NSUInteger currentState = 0;
...
currentState |= button.tag;

ボタンが選択されていない場合、bitwise-ANDそのタグは現在の状態と逆になります。

currentState &= ~button.tag;

XOR状態を切り替えるには、現在の状態でタグ付けできます。

currentState ^= button.tag;

選択済み/非選択状態をボタンに再適用する必要がある場合は、次のようなループで実行できます。

for (int i = 0 ; i != 28 ; i++) {
    NSUInteger tag = 1<<i;
    if (storedState & tag) {
        UIButton *btn = [myView viewWithTag:tag];
        // ... make the button selected ...
    }
}
于 2012-08-21T17:44:34.153 に答える
0

あなたの質問が正しければ、答えは簡単です: Key-Value-Coding を見てください - それはあなたを助けるはずです!

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html

于 2012-08-21T17:40:32.810 に答える