0

私は、いくつかのセルに UISwitch を持つ UITableView を持っています。

イベントをキャプチャしたいのですが、indexPath を追加しようとするとクラッシュします。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 //Create cell with all data

    UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchview;
    [switchview addTarget:self action:@selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];
}


- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath {

   UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
   UISwitch *switchView = (UISwitch *)cell.accessoryView;

   if ([switchView isOn]) {
       NSLog(@"ON");
   } else {
       NSLog(@"OFF");
   }

}

indexPath パラメータを追加していないためだと思いますが、設定方法がわかりません。

 -[ParkingData updateSwitchAtIndexPath]: unrecognized selector sent to instance 0x7b88eb0

ありがとう!

4

3 に答える 3

1

アップデート

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 //Create cell with all data

    UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchview;
    [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)updateSwitchAtIndexPath:(UISwitch *)switchview {
   if ([switchView isOn]) {
       NSLog(@"ON");
   } else {
       NSLog(@"OFF");
   }

}
于 2012-08-30T07:47:28.553 に答える
1
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];

する必要があります

[switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];

コロンだけがありません。メソッドにはパラメーターがあるためです。

- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath
于 2012-08-30T07:41:53.460 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchview;
    [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateSwitchAtIndexPath:(id)sender
{
   // see the line BELOW
   NSIndexPath *indexPath = [[(UITableView*)[sender superview] superview]indexPathForCell: (UITableViewCell*)[sender superview]];

    if ([sender isOn])
        NSLog(@"ON");
    else
        NSLog(@"OFF");
}

上記のコード送信者はUISwitchを意味し、セルのindexPathが必要な場合は、マークされた行が役立ちます。

于 2012-08-30T10:18:55.480 に答える