4

使用这个论坛中的精彩帖子,我在 tableView 中创建了一个开关作为附件视图。当触摸开关时,我的动作(switchChanged)被调用。只有发送者有一个有效值,事件是0x0。

将目标添加到 switchView:

        [switchView addTarget:self action:@selector(switchChanged:forEvent:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];

那个行动:

- (void) switchChanged:(id)sender forEvent:(UIEvent *)event {
  if(event) {
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:self.tableView]];
    IFDFlightlogFormQuestions *question = [self.resultsController objectAtIndexPath:indexPath];
    NSLog(@"IFDNewFlightlogViewController_Pad:switchChanged Switch is %@ xmlAttrib %@",[sender isOn],question.XmlAttrib);
    [self.xmlResults setValue:([sender isOn])?@"true":@"false" forKey:question.XmlAttrib];
  }
}

使用action:@selector(switchChanged: forEvent:)- 增加空间 - 没有变化。

使用action:@selector(switchChanged::)- 移除forEvent- 无法识别的选择器。

我的目标是获取 tableView 的 indexPath,以便我可以更改字典中的值。

我目前的解决方法是只使用发件人信息,但我想要事件信息:

- (void) switchChanged:(id)sender forEvent:(UIEvent *)event {
    UISwitch *switchView = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)switchView.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    IFDFlightlogFormQuestions *question = [self.resultsController objectAtIndexPath:indexPath];
    NSLog(@"IFDNewFlightlogViewController_Pad:switchChanged Switch is %@ xmlAttrib %@",([sender isOn])?@"On":@"Off",question.XmlAttrib);
    [self.xmlResults setValue:([sender isOn])?@"true":@"false" forKey:question.XmlAttrib];        
}

有关获取有关此事件信息的任何指示吗?

4

3 回答 3

6

可以@selector通过将目标添加到UISwitch.

[switchCtl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
于 2013-12-21T01:10:53.303 回答
2

另一种可能性是在 UISwitch 上设置 tag 属性以匹配 tableview 的行。如果您也使用多个部分,那么您可以想出一些解决方案来将它们编码为一个整数值。请记住,使用这种方法,您需要在每次更新单元格时更新标记 (tableView:cellForRowAtIndexPath:),因为当单元格被重用时,行可能会发生变化。

于 2011-06-02T16:19:24.583 回答
0

不幸的是,没有forEvent:从 UIControl 操作发送......只有第一部分,这就是为什么你没有看到任何价值。

您的解决方法的工作方式是一种合适的方法。

另一种方法是将 UISwitch 子类化,以便它持有对 NSIndexPath 的引用,甚至覆盖

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

中断并发送自定义事件...

于 2011-05-02T22:08:31.663 回答