【问题标题】:Select UITableView's row when clicking on UISwitch点击 UISwitch 时选择 UITableView 的行
【发布时间】:2012-10-18 17:43:37
【问题描述】:

我有一个UITableView,上面有UISwitchs。

当切换开关时,我想运行一个函数。该功能只记录开关是否打开以及开关已打开的行。我遇到的问题是,当我单击开关时,它不会记录正确的行,除非我在单击开关之前单击了该行。

我想我的问题是单击开关不会选择行。我怎样才能使它选择行或者我可以将 ID 添加到开关?

所以开关 ID“1”为“ON”。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"POICell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    //set the cell text to the catName
    cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
    //add switch 
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchView;
    [switchView setOn:YES animated:NO];

    [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
    // Configure the cell...

    return cell;
}

     - (void) switchChanged:(id)sender {
            NSString *StrCatID =[[NSString alloc]init];
            StrCatID = [self.catIDs objectAtIndex:[self.inputTableView indexPathForSelectedRow].row];
            UISwitch* switchControl = sender;
            NSLog( @"The switch for item %@ is %@",StrCatID, switchControl.on ? @"ON" : @"OFF" );
        }

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    找到持有开关的单元格

    UISwitch *switchInCell = (UISwitch *)sender;
    UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
    

    查找该单元格的索引路径

    NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
    

    你的情况

     - (void) switchChanged:(id)sender {
    
             UISwitch *switchInCell = (UISwitch *)sender;
             UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
             NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
             NSString *strCatID =[[NSString alloc]init];
             strCatID = [self.catIDs objectAtIndex:indexpath];
             NSLog( @"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF" );
            }
    

    【讨论】:

    • 在 iOS7 中,superview 将是一个 UITableViewCellContentView,它被包裹在一个 UITableViewCellScrollView 中。您可以使用以下命令获取单元格的索引路径:CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]]; NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint];
    【解决方案2】:

    您应该将IndexPath.row 设置为cellForRowAtIndexPath 方法中每个Switch 的标签

     switchView.tag= indexPath.row;
    

    当开关值改变时,你会得到行号

    - (void) switchChanged:(UISwitch *)sender {
      int rowIndex =[sender tag];
      //rowIndex you may use it further as you wanted.   
    

    }

    【讨论】:

    • 我认为这是迄今为止最好的方法。查找视图层次结构的其他方法,更糟糕的是,将一个点转换为一个视图,感觉很hackish并且喜欢做很多工作。使用此解决方案可能遇到的唯一问题是如果您有多个部分,但这可以通过使标签同时编码部分和行来解决。像这样:switchView.tag = indexPath.section * 1000 + indexPath.row;
    【解决方案3】:

    您可以检索在 tableview 中更改的 UISwitch 的 NSIndexPath。这与本文中已经回答的任何控件的想法相同:Detecting which UIButton was pressed in a UITableView

    - (void) switchChanged:(id)sender 
    {
        CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]];
        NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint];
    }
    

    这适用于 iOS7,之前我使用 [sender superview],但现在返回 UITableViewCellScrollView 内的 UITableViewCellContentView。

    【讨论】:

      【解决方案4】:

      我必须加倍 mySwitch.superview.superview 才能获得正确的单元格。

      这是一个例子

      - (void)switchToggle:(UISwitch *)mySwitch
      {
      
       UITableViewCell *cell = (UITableViewCell *)mySwitch.superview.superview;
       NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];
       NSLog(@"toggle section %d rowID %d", indexpath.section, indexpath.row);
      
      }
      

      【讨论】:

        【解决方案5】:

        我认为问题在于您使用 dequeueReusableCellWithIdentifier,并且您的所有单元格都具有相同的 id。

        【讨论】:

        • 如果我使用 [self.inputTableView indexPathForSelectedRow].row Id 给我选择的行。
        【解决方案6】:

        更好的方法是确定发件人所在的单元格。

        - (UITableViewCell *)findCellForView:(UIView *)view
        {
            for (; view != nil; view = view.superview)
                if ([view isKindOfClass:[UITableViewCell class]])
                    return view;
            return nil;
        }
        

        一旦你有了这个方法。那么只需将[self.inputTableView indexPathForSelectedRow] 替换为

        UITableViewCell *cell = [self findCellForView:sender];
        NSIndexPath *indexPath = [self.inputTableView indexPathForCell:cell];
        

        【讨论】:

          【解决方案7】:
          #import <UIKit/UIKit.h>
          
          @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
          @property (retain, nonatomic) IBOutlet UITableView *tableview;
          
          @end
          
          
          @interface ViewController ()
          {
              NSMutableArray *arr;
              UITableViewCell* aCell;
              UISwitch *myswitch;
          }
          
          
          #import "ViewController.h"
          
          @interface ViewController ()
          {
              NSMutableArray *arr;
              UITableViewCell* aCell;
              UISwitch *myswitch;
          }
          
          @end
          
          @implementation ViewController
          @synthesize tableview;
          
          - (void)viewDidLoad
          {
              [super viewDidLoad];
          
              arr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
              // Do any additional setup after loading the view, typically from a nib.
          }
          
          - (void)didReceiveMemoryWarning
          {
              [super didReceiveMemoryWarning];
              // Dispose of any resources that can be recreated.
          }
          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
          {
              return [arr count];
          }
          
          
          
          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
          {
              aCell = [tableview dequeueReusableCellWithIdentifier:@"SwitchCell"];
          
              if( aCell == nil )
                {
                  aCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
                  aCell.textLabel.text = [arr objectAtIndex:indexPath.row];
                  myswitch = [[UISwitch alloc]initWithFrame:CGRectZero];
                  aCell.accessoryView = myswitch;
                  [myswitch setOn:YES animated:YES];
          
                 }
              switch (indexPath.row)
          
              {
                  case 0:
                  {
                      [myswitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
          
                  }
                      break;
                  case 1:
                  {
                      [myswitch addTarget:self action:@selector(switchChanged1:) forControlEvents:UIControlEventValueChanged];
                  }
                      break;
          
              }
              return aCell;
          }
          
          - (void) switchChanged:(id)sender {
              UISwitch *aswitch = sender;
          
              if (aswitch.on==YES) {
                  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hello" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil];
                  [alert show];
              }
              else
              {
                  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no1" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil];
                  [alert show];
          
              }
          
          }
          - (void) switchChanged1:(id)sender {
              UISwitch *aswitch1 = sender;
          
              if (aswitch1.on==YES) {
                  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"second" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil];
                  [alert show];
              }
              else
              {
                  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no2" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil];
                  [alert show];
          
              }
            }
          
          
          - (void)dealloc {
              [tableview release];
              [super dealloc];
          }
          @end
          

          【讨论】:

          • 不要发布纯代码答案。请添加解释。
          • 在没有解释的情况下将一堆代码添加到一年前的帖子中并没有真正的帮助。请解释一下,以便其他人看到这可能会有所帮助
          猜你喜欢
          • 2015-06-13
          • 2021-07-27
          • 1970-01-01
          • 2016-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多