UITableViewのセルを管理するためのTableCellViewControllerがあります。各セルには、UISwitch(dictTypeSwitch)というラベルが付いています。ボタンの状態を保存できるように、イベントを切り替えるメソッドを割り当てたい。
これまで私はこれを行いました:
setState関数をオブジェクトに割り当てます。
[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];
イベントを処理するための関数:
- (void)setState:(id)sender {
BOOL state = [sender isOn];
NSString *rez = state == YES ? @"YES" : @"NO";
NSLog(rez);
}
送信者から、状態を取得できるUISwitchオブジェクトを取得します。
これまでのところ、すべてが大丈夫です。
ただし、UISwitchの状態を保存する場合は、このセルのrowIndexを取得する必要があります。どうすればこれを達成できますか?
The cells are made inside function cellForRowAtIndexPath. Se the code bellow:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"DictionariesTableCellViewController";
DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DictionariesTableCellViewController" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DictionariesTableCellViewController *) currentObject;
break;
}
}
}
// Configure the cell...
cell.dictTypeLabel.text = [NSString stringWithFormat:@"row - %i",indexPath.row];
[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
ありがとう