2012-02-24 13 views
1

トグルスイッチのaccessoryviewを持つテーブルビューがあります。私はセクションと行を指定して、どの行がトグルされたのかを判断するのが困難です。私はtoggleSwitch.tagを使ってindexRowを取得しましたが、indexRowはindexPath.sectionの一部であるため、どの行をトグルしたかをわかりません。ここでどのトグルスイッチが変更されたかを確認する方法

はコードです:ここでは

- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell; 

Category *cat = [allCategories objectAtIndex:indexPath.section]; 
Subject *sub = [cat.subjects objectAtIndex:indexPath.row]; 

cell = [[[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
UISwitch *toggleSwitch = [[UISwitch alloc] init]; 
cell.accessoryView = [[UIView alloc] initWithFrame:toggleSwitch.frame]; 
[cell.accessoryView addSubview:toggleSwitch]; 

cell.textLabel.text =sub.title; 
cell.detailTextLabel.text = sub.category_title; 

if (sub.active==1){ 
    [toggleSwitch setOn:YES]; 
} else { 
    [toggleSwitch setOn:NO]; 
} 

toggleSwitch.tag = indexPath.row; 

[toggleSwitch addTarget:self action:@selector(viewButtonPushed:) forControlEvents:UIControlEventValueChanged]; 
[toggleSwitch release]; 

return cell; 

} 


- (void)viewButtonPushed:(id)sender { 
UIButton *button = (UIButton *)sender;  
UITableViewCell *cell = button.superview; // adjust according to your UITableViewCell-subclass' view hierarchy 
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

Category *cat = [allCategories objectAtIndex:indexPath.section]; 
Subject *sub = [cat.subjects objectAtIndex:indexPath.row]; 

selectedSubject = sub; 

UISwitch* switchControl = sender; 
NSLog(@"The switch is %@", switchControl.on ? @"ON" : @"OFF"); 

if(switchControl.on){ 
    [sub setActive:1]; 
    NSLog(@"%@ is being set to ACTIVE", selectedSubject.title); 
}else{ 
    [sub setActive:0]; 
    NSLog(@"%@ is being set to INACTIVE", selectedSubject.title); 
} 

[sub setIsDirty:YES]; 

[cat.subjects replaceObjectAtIndex:indexPath.row withObject:sub]; 

[sub autorelease]; 
[cat autorelease]; 

} 

は私didSelectRowAtIndexPathです。トグルスイッチを参照する必要がありますか?

- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

Category *cat = [allCategories objectAtIndex:indexPath.section]; 
Subject *sub = [cat.subjects objectAtIndex:indexPath.row]; 

selectedSubject = sub; 
NSLog(@"selectedSubject = %@", selectedSubject.title); 


if (tableAlert.type == SBTableAlertTypeMultipleSelct) { 
    UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryNone) 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    else 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 

    [tableAlert.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

} 
+0

あなたは、あなたがPLISTまたは同様にそれを保存している場合は、ディスクへの変更を記述する必要があります知っています。配列を変更しただけでファイル自体は変更されません。 –

+0

そうですね。さらなるデバッグでは、私の問題は配列への保存ではなく、どの行が切り替えられたかの識別に問題があることがわかりました。 – jroyce

+2

これを行うきちんとした方法について私の答えを見てください:http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – jrturton

答えて

1

私はあなたが、細胞内のアイテムのスーパーのスーパーに行く必要があることを見出した(ボタンやコントロールが右のセルのルートから外れていると仮定して)へのポインタを取得するために、細胞。

UITableViewCell *cell = button.superview.superview; 

をし、結果がどの優れているかどうかを確認:

代わりにこれを試してみてください。詳細については、この上の私のブログの記事をチェックアウト:

Two superviews are better than one

+0

それは変です...前に見たことはありませんでした。 –

関連する問題