簡単な問題があります。Interface Builder で UISwitch を含むカスタム UITableViewCell を作成しました。
質問 1: より簡単で良い方法はありますか? 質問 2: UITableViewController で使用する場合、データを取得する最良の方法は何ですか? ある時点で、テーブルを調べてステータスに関するすべてのセルを確認するか、スイッチのステータスが直接変化したときにフィードバックを送信する必要がありますか?
助けてくれてありがとう。
簡単な問題があります。Interface Builder で UISwitch を含むカスタム UITableViewCell を作成しました。
質問 1: より簡単で良い方法はありますか? 質問 2: UITableViewController で使用する場合、データを取得する最良の方法は何ですか? ある時点で、テーブルを調べてステータスに関するすべてのセルを確認するか、スイッチのステータスが直接変化したときにフィードバックを送信する必要がありますか?
助けてくれてありがとう。
UISwitch
アクセサリ ビューに を追加するだけです。「エレガント」に見えることは言うまでもなく、これが最も簡単な方法です。
次に、テーブルビュー コントローラー コードで、スイッチが切り替えられるたびにセレクターを呼び出すか、コントローラーでスイッチの現在のステータスを取得してスイッチ自体を切り替えることもできます。
コードサンプルが必要な場合はお知らせください。
---サンプルコード---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//add a switch
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchview;
[switchview release];
}
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}
次に、モデルの変更に基づいてスイッチを更新するメソッドを作成できます。デリゲート、通知、KVO など、何でも使用できます。
例:
- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UISwitch *switchView = (UISwitch *)cell.accessoryView;
if ([switchView isOn]) {
[switchView setOn:NO animated:YES];
} else {
[switchView setOn:YES animated:YES];
}
}
Switch.tag = indexPath.row;
[Switch addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
- (void)updateSwitchAtIndexPath:(UISwitch *)aswitch{
FFLog(@"%i",aswitch.tag);
}
ありがとう。indexPathはありませんが、次のようなタグでセルまたはオブジェクトを識別できます。
if (indexPath.row == 0) {
cell.textLabel.text = @"Twitter";
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
switchview.tag = 111;
//switchview.selected = 0;
cell.accessoryView = switchview;
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
//cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
//[switchview release];
}
- (void)updateSwitchAtIndexPath {
for (UITableViewCell * cell in tblView.subviews) {
for (UISwitch *swch in cell.subviews) {
UISwitch *switchView = (UISwitch *)cell.accessoryView;
if (switchView.tag == 111) {
if ([switchView isOn]) {
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ON");
}else{
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OFF");
}
}
if (switchView.tag == 222) {
if ([switchView isOn]) {
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ON");
}else{
NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OFF");
}
}
}
}
}
2 つのオプション:
1) データ モデルへのポインタを使用してセルを初期化します。セルにスイッチのデリゲート コールバックを実装させます。スイッチが変更されたら、データ モデルへのポインターを使用して、新しい設定を反映します。または、ビュー コントローラーへのポインターを使用してセルを初期化します。セルに switch デリゲート メソッドを実装してから、適切なセル コンテキスト情報を使用してビュー コントローラーにコールバックします。
2) ビュー コントローラーにスイッチ デリゲート コールバックを実装させます。スイッチが変更されたら、それがどのスイッチ/セルであったかを判断し (スイッチの「タグ」プロパティを行番号などに設定する必要がある場合があります)、View Controller のコンテキストで更新を処理します。