UITableViewCell
はUILabel
とUISwitch
です。デフォルトでは、すべてUISwitch
がオフに設定されています。 //以下はUITableViewセルが再生中です
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell != nil) cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 0) {
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
lbl1.text = @"Some Text";
[cell addSubview:lbl1];
switch = [[UISwitch alloc] initWithFrame:CGRectMake(190, 10, 200, 30)];
[switch setOn:NO];
switch.tag = 1;
[switch addTarget:self action:@selector(switchTapped:) forControlEvents:UIControlEventChanged];
[cell addSubview:switch];
}
}
:
私はスイッチをオンにして、スイッチの値が再びデフォルトに設定されているテーブルをスクロールしますたら、下記
ieOff
は、私が使用しているコードです私のswitchTappedメソッドです:- (void) switchTapped: (id)sender {
UISwitch *tapSwitch = (UISwitch *)sender;
switch (tapSwitch.tag) {
case 1:
if (tapSwitch.on) {
// do something
}
else {
// do something
}
break;
case 2:
if (tapSwitch.on) {
// do something
}
else {
// do something
}
break;
}
私はここで何か間違っていますか?
ありがとうございます。
if(cell != nil)
{
cell = nil;
}
if (cell == nil)
{
...
}
はあなたには、いくつかの留保対象に、あなたのスイッチの状態をバインドしています(例:項目モデルオブジェクト、単一のセルが反映:あなたはそれが必要だたびにセルを再生成し、本当に厄介なコードを使用している
私はそれを使用していない場合、ラベルはあなたが、あなたの答えを持ってそこ – spaleja
を上書きしている:)保持対象とする場合、セルはちょうどそれを再利用する必要があるにあなたのスイッチの状態をバインドするようにしてください。 –
どうすればいいですか? – spaleja