单击UISwitch时选择UITableView的行

时间:2012-10-29 11:54:56

标签: iphone objective-c ios xcode

我有UITableView UISwitchs

TableView

当切换开关时,我想运行一个功能。该功能仅记录如果开关打开或关闭以及开关已更改的行。我遇到的问题是当我点击开关时它没有记录正确的行,除非我在单击开关之前点击了该行。

我想我的问题是单击开关不会选择行。如何才能选择行或者我可以将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" );
        }

7 个答案:

答案 0 :(得分:10)

查找包含开关的单元格

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" );
        }

答案 1 :(得分:9)

您应该将IndexPath.row设置为Switch方法

中每个cellForRowAtIndexPath的标记
 switchView.tag= indexPath.row;

当切换值改变时,你会得到行号

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

}

答案 2 :(得分:8)

您可以检索在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]但现在返回UITableViewCellScrollContentView内的UITableViewCellContentView。

答案 3 :(得分:5)

我必须做双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);

}

答案 4 :(得分:1)

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

答案 5 :(得分:1)

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

- (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];

答案 6 :(得分:-1)

#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